dynamonet/redis
Composer 安装命令:
composer require dynamonet/redis
包简介
Redis extension wrapper, with nice Lua-scripting support, and other helpful utilities
README 文档
README
A wrapper for the phpredis extension, with nice pipelining interfaces, Lua-scripting support, and other helpful utilities.
Install
composer install dynamonet/redis
Initialization
use Dynamo\Redis\Client as RedisClient; $redis = new RedisClient([ 'host' => 'localhost', 'port' => 6379, //'timeout' => xxx, //'retry_interval' => xxx, // ]); // you can now start sending commands, without the need for an explicit "connect" call $redis->set('mykey', 'value');
Pipelining
$result = $redis->pipeline() ->set('key1', 'value1') ->incr('key2') ->get('key2') ->exec(); var_dump($result); // an array with the result for every queued command
Redis 7 Functions
Redis 7 introduced the concept of "functions", which are named scripts, tipically written in Lua, which you invoke by the name you choose, like so:
FCALL myHelloFunction 1 "John Snow"
Prior to Redis functions, the only way to invoke a custom script was using the SHA1 string returned by the LOAD SCRIPT command 🤮
This library already has support for Redis 7 functions. All you have to do is register your functions, and then invoke them as if they were existing commands on the client.
// Register my custom functions $redis->loadFunctionScript($path_to_my_functions_script, [ 'functionA' => 1, 'functionB' => 2, 'functionC' => 0, ]);
Notice that associative array passed as the second argument. That argument is mandatory, and is the only way we have to tell the client which functions are exposed in the script, and how many key (mandatory) arguments have each of our custom functions.
// Invoking my functions $redis->functionA('Hello'); $redis->functionB('World', 2); $redis->functionA();
Lua scripting
Lua scripts in Redis are like traditional database Stored Procedures, but on steroids, because you write them in Lua, so you may use conditionals, loops, and all the flow control structures available in the Lua language. The only downside of Redis scripting (if your are using any version older than 7) is having to use a SHA1 string to invoke your scripts. Fortunately, this library will make your Lua scripting much more pleasant.
Recomended use: have your Lua scripts in separate .lua files, load them to your client, and assign each script an alias:
use Dynamo\Redis\LuaScript; $redis->loadScript( LuaScript::fromFile( $path_to_my_lua_script, 2, // amount of mandatory arguments 'myluacmd' //alias for the script ), true //register the script NOW. If "false" is provided, it will be lazy-loaded ); //now, you can call your Lua command as if it were a regular Redis command $redis->myluacmd('blablabla', 2); //you can also call your custom commands in a pipeline $result = $redis->pipeline() ->set('key1', 'value1') ->incr('key2') ->get('key2') ->myluacmd('blablabla', 2) ->exec();
dynamonet/redis 适用场景与选型建议
dynamonet/redis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.05k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dynamonet/redis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dynamonet/redis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-19