nailfor/redis 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nailfor/redis

Composer 安装命令:

composer require nailfor/redis

包简介

Eloquent like library for Redis

README 文档

README

Redis client for Eloquent ORM

Features

All models are inherited from Illuminate\Database\Eloquent\Model so most methods work natively

Model Supports

Method Is Working
CURD Yes
Condition Select yes(HASH)
filling Yes
Limit Not yet
Chunking Not yet
Transaction Not yet
Insert a lot of data Not yet
Delete a lot of data Not yet
Update a lot of data Not yet
Expire Yes
Relationship Yes

Key Supports

Model type Constant
HASH TYPE_HASH
SET TYPE_SET

Key structure

Sample key structure for a Redis model in Laravel:

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

  • model_table_name: The name of the current model table which set like 'protected $table = "name"'.
  • primary_key: The primary key of the model (id).

Example key:

rdb_product:1

Installation

The preferred way to install this extension is through composer.

Either run

composer require nailfor/redis

or add

"nailfor/redis" : "*"

to the require section of your application's composer.json file.

Configure

Add config/app.php

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

and config/database.php

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

Usage

Models

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

DbProduct is a regular Eloquent model.

<?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 is a redis cache for DbProduct

<?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 some Redis model for Brands

<?php

namespace App\Models;

use nailfor\Redis\Eloquent\Model;

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

Insert

    $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();

Or, because we set $fillable

    $product = DbProduct::find(1);

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

Retrieving Models

    $product = RdbProduct::find(2);
    //or
    $product = RdbProduct::where('id', 2)
        ->whereIn('brand_id', [1,2,3])
        ->orWhere('article', 'SV-FX-02')
        ->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();
    }

Delete Models

    //truncate all product:*
    RdbProduct::delete();

    //deleting by condition
    RdbProduct::where('brand_id', 4)->delete();
    RdbProduct::where('id', 2)
        ->orWhere('brand_id', 5)
        ->delete();
    RdbProduct::where('article', 'VGX-01')
        ->whereIn('brand_id', [1,2,4])
        ->delete()
    ;

    //deleting single model
    $model = RdbProduct::find(1);
    $model->delete();

Expire and 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

Credits

License

The GNU License (GNU). Please see License File for more information.

nailfor/redis 适用场景与选型建议

nailfor/redis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 654 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nailfor/redis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nailfor/redis 我们能提供哪些服务?
定制开发 / 二次开发

基于 nailfor/redis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 654
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-07