inhere / redis
此包已被弃用,不再维护。未建议替代包。
PHP 的简单 redis 库
v0.5.1
2017-03-02 15:04 UTC
Requires
- php: >=5.5.0
- ext-redis: *
Suggests
- inhere/php-validate: Very lightweight data validate tool
- inhere/simple-print-tool: Very lightweight data printing tool
This package is not auto-updated.
Last update: 2020-01-24 16:38:40 UTC
README
PHP 的简单 redis 客户端库
项目
安装
注意:需要 php 扩展 'redis'
- 使用 composer
编辑 composer.json
文件,在 require 部分添加
"inhere/redis": "dev-master",
运行: composer update
- 直接获取
git clone https://github.com/inhere/php-redis.git // github
git clone https://git.oschina.net/inhere/php-redis.git // git@osc
配置
单例配置
$config = [ 'host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0.0, 'database' => 0, ];
主从配置
$config = [ 'mode' => 2, // 1 singleton 2 master-slave 3 cluster 'master' => [ 'host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0.0, 'database' => 0, ], 'slaves' => [ 'slave0' => [ 'host' => '127.0.0.1', 'port' => 6380, 'timeout' => 0.0, 'database' => 0, ] ], ];
集群配置
$config = [ 'mode' => 3, // 1 singleton 2 master-slave 3 cluster 'name1' => [ 'host' => '127.0.0.1', 'port' => '6379', 'database' => '0', 'options' => [] ], 'name2' => [ 'host' => '127.0.0.2', 'port' => '6379', 'database' => '0', 'options' => [] ], ];
创建客户端
use inhere\redis\ClientFactory; // $app is my application instance. $client = ClientFactory::make($config);
添加事件监听
$client->on(ClientInterface::CONNECT, function($name, $mode, $config) { printf("CONNECT:connect to the name=%s,mode=%s,config=%s\n", $name, $mode, json_encode($config)); }); $client->on(ClientInterface::DISCONNECT, function($name, $mode) { $names = 'all'; if ($name) { $names = is_array($name) ? implode(',', $name) : $name; } printf("DISCONNECT:close there are %s connections,mode=%s\n", $names, $mode); }); $client->on('beforeExecute', function ($cmd, array $args, $operate) { printf("BEFORE_EXECUTE:will be execute the command=$cmd, operate=$operate, args=%s\n", json_encode($args)); }); $client->on('afterExecute', function ($cmd, array $data, $operate) { printf("AFTER_EXECUTE:has been executed the command=$cmd, operate=$operate, data=%s\n", json_encode($data)); });
用法
echo $client->ping(); // +PONG echo "test set/get value:\n"; $suc = $client->set('key0', 'val0'); // bool(true) $ret0 = $client->get('key0'); // string(4) "val0" var_dump($suc, $ret0); echo "test del key:\n"; $suc = $client->del('key0'); // int(1) $ret0 = $client->get('key0'); // bool(false) var_dump($suc, $ret0);
更多
请参考 示例
许可
MIT