limen/redisun 问题修复 & 功能扩展

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

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

limen/redisun

Composer 安装命令:

composer require limen/redisun

包简介

Make redis manipulations easy. Unify commands for all data types.

README 文档

README

Build Status Packagist

中文

Wiki

Python version

Features

  • Unified commands for all data types: string, list, hash, set and zset.
  • support SQL like query
  • use "eval" to save time consumption on network.
  • "set" like commands all support to set new ttl or keep current ttl

Unified commands

  • create: create key
  • createNotExists: create key when which not exists
  • createExists: create key when which exists
  • insert: similar to create except supporting multiple keys
  • insertNotExists: similar to createNotExists
  • insertExists: similar to createExists
  • get: get key to replace get, lrange, hgetall, smembers and zrange
  • getAndSet: get key and set new value
  • find: similar to get
  • findBatch: find batch
  • update: update keys
  • destroy: remove one key
  • destroyBatch: remove keys
  • delete: remove keys

Installation

Recommend to install via composer.

composer require "limen/redisun"

Usage

use Limen\Redisun\Examples\HashModel;
use Limen\Redisun\Examples\StringModel;

$person = [
   'name' => 'martin',
   'age' => '22',
   'height' => '175',
   'nation' => 'China',
];
$hashModel = new HashModel();
$hashModel->create(1, $person);
$hashModel->find(1);                    // return $person
$hashModel->where('id',1)->first();     // return $person
$hashModel->where('id',1)->get();       // return ['redisun:1:hash' => $person]
$hashModel->where('id',1)->delete();    // remove key "redisun:1:hash" from database

$nick = 'martin-walk';

$stringModel = new StringModel();
$stringModel->insert([
    'id' => 1,
    'name' => 'martin'
], $nick);
$stringModel->where('id',1)->first();   // return $nick
$stringModel->where('id',1)->get();     // return ['redisun:1:string:martin' => $nick]

Concepts

Key representation

Every model has its own key representation which tells how to build query keys. For example

school:{schoolId}:class:{classId}:members

We can use where clauses to query the Redis.

$model->where('schoolId',1)->whereIn('classId',[1,2])->get();

The keys to query are

school:1:class:1:members
school:1:class:2:members

Key field

Key field is a dynamic part of the key representation.

Take the key representation above, it has two fields

  • schoolId
  • classId

Complete key

When a key has no unbound field, we treat it as complete. For example

school:1:class:2:members

On the contrary, an incomplete key is similar to

school:1:class:{classId}:members

Returned data set

The returned data set would be an associated array whose indices are the query keys.

When both keys exist on Redis database, the returned data set would be

[
    'school:1:class:1:members' => <item1>,
    'school:1:class:2:members' => <item2>,
]

If a key not exist, the equivalent index would be not set.

The returned item's data type depends on the model's type which could be string, hash, list, set or zset.

  • string: string
  • hash: associated array
  • list: array
  • set: array
  • zset: array

Methods

create

Can use when a model's key representation has only one dynamic field as its primary field.

The item's ttl is optional.

Hash type with key representation

user:{id}:info
$model->create(1, [
    'name' => 'maria',
    'age' => 22,
], 10);   // the item "user:1:info" would expire after 10 seconds

zset type with key representation

shop:{id}:customers
// key -> member, value -> score
$model->create(1, [
    'maria' => 1,
    'martin' => 2,
]);   // the item "shop:1:customers" would not expire

createExists

Similar to "setxx" but supports more data types: string, hash, set, zset and list.

createNotExists

Similar to "setnx" but supports more data types.

insert

An optional parameter make it possible to insert like "setnx" and "setxx". String type with key representation.

user:{id}:code
$model->insert([
    'id' => 1,
], 10010, 20); // the item "user:1:code" would expire after 20 seconds 

insertExists

Similar to createExists

insertNotExists

Similar to createNotExists

find

Can use when a model's key representation has only one dynamic field as its primary field.

$model->find(1);

findBatch

Similar to find. The returned data set are indexed by ids.

$model->findBatch([1,2,3]);
// [
//     1 => <item1>,
//     2 => <item2>,
//     3 => <item3>,
// ]

updateBatch

Similar to findBatch.

The key would be created if not exist. The key's ttl would not be modified if the ttl parameter not set.

$model->updateBatch([1,2,3], $value);

all

key representation

user:{id}:code
$model->all();      // return all keys which match pattern "user:*:code"

where

Similar to SQL

$model->where('id', 1)->where('name', 'maria');

whereIn

Similar to where

$model->whereIn('id', [1,2,3]);

first

Get first exist item from query keys. Return null when all query keys not exist.

$model->whereIn('id', [1,2,3])->first();    // return string|array|null

update

The key would be created if not exist. The key's ttl would not be modified if the ttl parameter not set.

$model->where('id',1)->update($value);

delete

Delete query keys.

$model->where('id',1)->delete();

orderBy, sort

string type with key representation

user:{id}:code
$model->insert([
    'id' => 1,
], 10010); 
$model->insert([
    'id' => 2,
], 10011); 

$model->whereIn('id', [1,2])->orderBy('id')->get();
// returned data set
// [
//     'user:1:code' => 10010,
//     'user:2:code' => 10011,
// ]
$model->newQuery()->whereIn('id', [1,2])->orderBy('id', 'desc')->get();
// returned data set
// [
//     'user:2:code' => 10011,
//     'user:1:code' => 10010,
// ]
$model->newQuery()->whereIn('id', [1,2])->sort();
// returned data set
// [
//     'user:1:code' => 10010,
//     'user:2:code' => 10011,
// ]

count

Count the exist query keys.

$model->where('id', 1)->count();    // return an integer

max

Get the maximum item in the returned data set.

$model->where('id', 1)->max();

min

Get the minimum item in the returned data set.

$model->where('id', 1)->min();

sum

Get the sum of the returned data set.

$model->where('id', 1)->sum();

Predis native methods

Predis native methods such as "sadd", "hset" can use when the query contains only one complete query key.

// string model
$model->where('id', 1)->set('maria');

// hash model
$model->where('id', 1)->update([
    'name' => 'Maria',
    'age' => '22',
]);
// equals to
$model->where('id', 1)->hmset([
    'name' => 'Maria',
    'age' => '22',
]);

Query builder

Taking the job to build query keys for model.

key representation

user:{id}:{name}
$queryBuilder->whereIn('id', [1,2])->whereIn('name', ['maria', 'cat']);
// built keys
// user:1:maria
// user:1:cat
// user:2:maria
// user:2:cat

$queryBuilder->refresh()->whereIn('id', [1,2]);
// built keys
// user:1:{name}
// user:2:{name}

Development

Test

$ phpunit --bootstrap tests/bootstrap.php tests/

limen/redisun 适用场景与选型建议

limen/redisun 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.69k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2018 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 4
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-18