angyvolin / predis-commands
为Predis添加一些有用的命令。
0.1.3
2017-05-01 13:56 UTC
Requires
- predis/predis: ^1.1
Requires (Dev)
- phpunit/phpunit: 5.4
This package is not auto-updated.
Last update: 2024-09-15 02:01:14 UTC
README
动机
由于Redis是单线程的,任何单个Redis命令总是原子的。在某些情况下,您可能希望原子地运行多个Redis命令。
实现此目的有两种常见方法
- Redis事务。它使用
MULTI
/EXEC
Redis命令按顺序运行一些命令。它还使用WATCH
/DISCARD
Redis命令进行CAS
乐观并发。 - Redis脚本。这个定义上是事务性的:Redis使用相同的Lua解释器运行所有命令,并保证在脚本执行期间不会执行其他脚本或Redis命令。
本项目使用Lua脚本方法,因为它通常既简单又快速。
安装
composer require angyvolin/predis-commands
包含什么?
到目前为止,predis-commands
项目通过以下命令扩展了Predis:
ZPOP - Removes and returns the top value in a zset, with its score.
ZRPOP - Removes and returns the bottom value in a zset, with its score.
如何使用predis-commands ...
... 在纯PHP中
以下代码片段演示了如何在不依赖任何框架的情况下使用predis-commands
<?php require __DIR__.'/../vendor/autoload.php'; use Angyvolin\Predis\Command; $client = new Predis\Client(); $client->getProfile()->defineCommand('zpop', Command\ZSetPop::class); $client->getProfile()->defineCommand('zrpop', Command\ZSetReversePop::class);
... 在Silex应用程序中
以下代码片段演示了与Silex应用程序的集成
<?php use Pimple\Container; use Pimple\ServiceProviderInterface; use Angyvolin\Predis\Command; class PredisCommandsServiceProvider implements ServiceProviderInterface { public function register(Container $app) { $app->extend('redis', function ($redis) { /* @var \Predis\Client $redis */ $redis->getProfile()->defineCommand('zpop', Command\ZSetPop::class); $redis->getProfile()->defineCommand('zrpop', Command\ZSetReversePop::class); return $redis; }); } }
测试
要运行测试,请在predis-commands项目的根目录中执行以下命令
phpunit