nailfor/redis

Redis 的 Eloquent 类库

v0.1.4 2024-07-16 11:05 UTC

This package is not auto-updated.

Last update: 2024-09-24 11:58:48 UTC


README

Redis 客户端用于 Eloquent ORM

特性

所有模型都继承自 Illuminate\Database\Eloquent\Model,因此大多数方法都可以原生使用

模型支持

键支持

键结构

Laravel 中 Redis 模型的示例键结构

{config.redis.options.prefix}{model_table_name|class_name}:{primary_key}

  • model_table_name:当前模型表名,如设置为 'protected $table = "name"'。
  • primary_key:模型的键(id)。

示例键

rdb_product:1

安装

推荐通过 composer 安装此扩展。

运行以下命令之一:

composer require nailfor/redis

或添加

"nailfor/redis" : "*"

到应用程序的 composer.json 文件的 require 部分。

配置

添加 config/app.php

    'providers' => [
        ...
        nailfor\Redis\RedisServiceProvider::class,

和 config/database.php

    'connections' => [
        ...
        'redis' => [ //the name of connection in your models(default)
            'driver'    => 'redis',
        ],

使用方法

模型

├── DbProduct.php
├── RdbBrand.php
├── RdbProduct.php
└── User.php

DbProduct 是一个常规 Eloquent 模型。

<?php

namespace App\Models;

use App\Models\DbCategory;
use App\Models\RdbBrand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

class DbProduct extends Model
{
    protected $table = 'product';

    //sql to sql relationship
    public function Category(): BelongsTo 
    {
        return $this->belongsTo(DbCategory::class, 'category_id');
    }

    //sql to redis relationship
    public function Brand(): HasOne
    {
        return $this->hasOne(RdbBrand::class, 'brand_id');
    }
}

RdbProduct 是 DbProduct 的 Redis 缓存

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use nailfor\Redis\Eloquent\Model;

class RdbProduct extends Model
{
    protected $table = 'product';

    protected $fillable = [
        'id',
        'name',
        'article',
        'brand_id',
        'category_id',
    ];

    //Since the model type is HSET, all fields are stored as a string.
    protected $casts = [
        'id' => 'integer',
        'brand_id' => 'integer',
    ];

    //redis to redis relationship
    public function Brand(): BelongsTo
    {
        return $this->belongsTo(RdbBrand::class, 'brand_id');
    }

    //redis to sql relationship
    public function Category(): BelongsTo
    {
        return $this->belongsTo(DbCategory::class, 'category_id');
    }
}

RdbBrand 是 Brands 的某些 Redis 模型

<?php

namespace App\Models;

use nailfor\Redis\Eloquent\Model;

class RdbBrand extends Model
{
    //without var $table the key will be "rdb_brand"
}

插入

    $product = DbProduct::find(1);

    $db              = new RdbProduct();
    $db->id          = $product->id;
    $db->name        = $product->name;
    $db->article     = $product->article;
    $db->brand_id    = $product->brand_id;
    $db->category_id = $product->category_id;
    $db->save();

或者,因为我们设置了 $fillable

    $product = DbProduct::find(1);

    $db = new RdbProduct();
    $db->fill($product->toArray());
    $db->save();

检索模型

    $product = RdbProduct::find(2);
    //or
    $product = RdbProduct::where('id', 2)->first();
    //get all products
    $products = RdbProduct::with([
            'Brand',
        ])
        ->get()
    ;

    //get only id 1,2,3...
    $products = RdbProduct::with([
            'Brand',
            'Category',
        ])
        ->whereIn('id', [1,2,3])
        ->get()
    ;

    foreach($products as $product) {
        $brand      = $product->Brand;      //relation to Redis model RdbBrand
        $category   = $product->Category;   //relation to SQL model DbCategory

        //of course u can modify this models here
        $brand->type = 'sometype';
        $brand->save();
    }

过期和 TTL

    $db = new RdModel;
    $db->id = 'key';
    $db->save();

    $timeInSeconds = 5;
    $db->expire($timeInSeconds);

    $db = new RdModel;
    $db->id = 'key';
    $ttl = $db->ttl(); //this op working w/o save model

致谢

许可证

GNU 许可证 (GNU)。请参阅 许可证文件 以获取更多信息。