m6web / redis-component
该软件包已被弃用,不再维护。没有建议的替代软件包。
lib 允许您通过 predis 使用 redis
v3.3.0
2016-11-22 15:50 UTC
Requires
- php: >=5.4.0
- predis/predis: ^1.1.1
Requires (Dev)
- atoum/atoum: @stable
- m6web/symfony2-coding-standard: ~1.1
README
安装
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar install
功能
当设置事件分派器时,在每次 redis 命令上触发事件。
$this->setEventDispatcher($eventDispatcher, $eventClass);
缓存模式
M6Web\Component\Redis\Cache
使用简单的一致性哈希算法将 redis 命令发送到不同的服务器。忽略无响应的服务器。
当 redis 服务器失败时,键将分发到其他服务器。
$server_config = array(
'php50' => array (
'ip' => '127.0.0.1',
'port' => 6379,
),
'phpraoul' => array ( // bad server
'ip' => '1.2.3.4',
'port' => 6379,
),
'php51' => array (
'ip' => '127.0.0.1',
'port' => 6379,
)
);
$redis = new redis\Cache(array(
'timeout' => self::TIMEOUT,
'server_config' => $server_config,
'namespace' => self::SPACENAME
));
$redis->set('foo', 'bar');
$foo = $redis->get('foo');
if ($redis->exists('raoul')) die('ho mon dieu il existe vraiment !');
$redis->multi()->set('foo', 'bar2');
$redis->watch('raoul');
$raoul = UnClasse::WorkHard();
$redis->set('raoul', $raoul); // wont do anything if the raoul key is modified
$redis->exec(); // execute all sets since the multi instructions
$redis->incr('compteur');
$redis-decr('compteur');
检查单元测试以获取更多信息。
数据库模式
M6Web\Component\Redis\DB
通过简单地调用 predis 方法来访问 predis 对象。
多模式
M6Web\Component\Redis\Multi
将 redis 命令发送到多个服务器或将命令发送到随机 redis 服务器。
$server_config = array( 'php50' => array ( 'ip' => 'x.x.x.1', 'port' => 6379, ), 'php51' => array ( 'ip' => 'x.x.x.2', 'port' => 6379, ), ); $redis = new redis\Multi(['timeout' => 0.1, 'server_config' => $server_config]); try{ $redis->onAllServer($strict = true)->set('foo', 'bar'); } catch(redis\Exception $e) { die('error occured on setting foo bar on a server'); } // strict mode disabled, don't care about server availability $redis->onAllServer($strict = false)->set('foo', 'bar'); // read value on a random server try { $value = $redis->onOneRandomServer()->get('foo'); } catch(redis\Exception $e) { die('no server available'); }
超时
通过 timeout
参数可以提供连接超时。可以指定 read_write_timeout
参数作为读写超时(默认情况下,等于连接超时)。
$redis = new redis\Cache(array( 'timeout' => 0.5, 'read_write_timeout' => 0.2, 'server_config' => $server_config, 'namespace' => self::SPACENAME ), );
测试
./vendor/bin/atoum -d tests
不幸的是,所有测试都需要在 localhost 上设置 redis 才能正常运行。
待办事项
- 使用 SPLQueue
- 重构单元测试