return -1; } public function getScript() { return <<<LUA local cmd, insert = redis.call, table.insert local increment, results = ARGV[1], { } for idx, key in ipairs(KEYS) do if cmd('exists', key) == 1 then insert(results, idx, cmd('incrby', key, increment)) else insert(results, idx, false) end end return results LUA; } } $client = new Predis\Client($single_server); $client->getProfile()->defineCommand('increxby', 'IncrementExistingKeysBy'); $client->mset('foo', 10, 'foobar', 100); var_export($client->increxby('foo', 'foofoo', 'foobar', 50)); /* array ( 0 => 60, 1 => NULL, 2 => 150, ) */
private function createRedisClient(array $parameters = array()) { $predisClient = new \Predis\Client($parameters, $this->options); $predisClient->getProfile()->defineCommand('role', '\\PSRedis\\Client\\Adapter\\Predis\\Command\\RoleCommand'); return $predisClient; }
<?php require 'vendor/autoload.php'; Predis\Autoloader::register(); $client = new Predis\Client(array('host' => '127.0.0.1', 'port' => 6379), array('prefix' => 'php:')); class MultiplyValue extends Predis\Command\ScriptCommand { public function getKeysCount() { return 1; } public function getScript() { $lua = <<<LUASCRIPT local value = redis.call('GET', KEYS[1]) value = tonumber(value) local newvalue = value * ARGV[1] redis.call('SET', KEYS[1], newvalue) return newvalue LUASCRIPT; return $lua; } } $client->getProfile()->defineCommand('multiply', 'MultiplyValue'); $client->set("mynumber", 4); $client->multiply("mynumber", 2); #8
/** * Creates predis client * @param array $parameters * @return \Predis\Client */ public function createClient(array $parameters = array()) { $predisClient = new \Predis\Client($parameters); $predisClient->getProfile()->defineCommand('sentinel', '\\Redis\\Client\\Adapter\\Predis\\Command\\SentinelCommand'); $predisClient->getProfile()->defineCommand('role', '\\Redis\\Client\\Adapter\\Predis\\Command\\RoleCommand'); return $predisClient; }