Merge branch 'command_processor'

This commit is contained in:
Daniele Alessandri
2011-05-05 16:31:55 +02:00
59 changed files with 386 additions and 311 deletions
+3 -3
View File
@@ -2,16 +2,16 @@
require 'SharedConfigurations.php';
// Predis ships with a KeyPrefixPreprocessor class that is used to transparently
// Predis ships with a KeyPrefixProcessor class that is used to transparently
// prefix each key before sending commands to Redis, even for complex commands
// such as SORT, ZUNIONSTORE and ZINTERSTORE. Key prefixes are useful to create
// user-level namespaces for you keyspace, thus eliminating the need for separate
// logical databases.
use Predis\Commands\Preprocessors\KeyPrefixPreprocessor;
use Predis\Commands\Processors\KeyPrefixProcessor;
$client = new Predis\Client();
$client->getProfile()->setPreprocessor(new KeyPrefixPreprocessor('nrk:'));
$client->getProfile()->setProcessor(new KeyPrefixProcessor('nrk:'));
$client->mset(array('foo' => 'bar', 'lol' => 'wut'));
var_dump($client->mget('foo', 'lol'));
+18
View File
@@ -18,6 +18,11 @@ abstract class Command implements ICommand {
unset($this->_hash);
}
public function setRawArguments(Array $arguments) {
$this->_arguments = $arguments;
unset($this->_hash);
}
public function getArguments() {
return $this->_arguments;
}
@@ -28,6 +33,19 @@ abstract class Command implements ICommand {
}
}
protected function onPrefixKeys(Array $arguments, $prefix) {
$arguments[0] = "$prefix{$arguments[0]}";
return $arguments;
}
public function prefixKeys($prefix) {
$arguments = $this->onPrefixKeys($this->_arguments, $prefix);
if (isset($arguments)) {
$this->_arguments = $arguments;
unset($this->_hash);
}
}
protected function canBeHashed() {
return isset($this->_arguments[0]);
}
+4
View File
@@ -7,6 +7,10 @@ class ConnectionAuth extends Command {
return 'AUTH';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ConnectionEcho extends Command {
return 'ECHO';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ConnectionPing extends Command {
return 'PING';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ConnectionQuit extends Command {
return 'QUIT';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ConnectionSelect extends Command {
return 'SELECT';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+1
View File
@@ -9,5 +9,6 @@ interface ICommand {
public function getHash(INodeKeyGenerator $distributor);
public function setArguments(Array $arguments);
public function getArguments();
public function prefixKeys($prefix);
public function parseResponse($data);
}
+4
View File
@@ -13,6 +13,10 @@ class KeyDelete extends Command {
return Helpers::filterArrayArguments($arguments);
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
$args = $this->getArguments();
if (count($args) === 1) {
+4
View File
@@ -7,6 +7,10 @@ class KeyRename extends Command {
return 'RENAME';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return false;
}
+1 -5
View File
@@ -2,15 +2,11 @@
namespace Predis\Commands;
class KeyRenamePreserve extends Command {
class KeyRenamePreserve extends KeyRename {
public function getId() {
return 'RENAMENX';
}
protected function canBeHashed() {
return false;
}
public function parseResponse($data) {
return (bool) $data;
}
+24
View File
@@ -52,4 +52,28 @@ class KeySort extends Command {
return $query;
}
protected function onPrefixKeys(Array $arguments, $prefix) {
$arguments[0] = "$prefix{$arguments[0]}";
if (($count = count($arguments)) > 1) {
for ($i = 1; $i < $count; $i++) {
switch ($arguments[$i]) {
case 'BY':
case 'STORE':
$arguments[$i] = "$prefix{$arguments[++$i]}";
break;
case 'GET':
$value = $arguments[++$i];
if ($value !== '#') {
$arguments[$i] = "$prefix$value";
}
break;
case 'LIMIT';
$i += 2;
break;
}
}
}
return $arguments;
}
}
@@ -7,6 +7,10 @@ class ListPopFirstBlocking extends Command {
return 'BLPOP';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::skipLastArgument($arguments, $prefix);
}
protected function canBeHashed() {
return $this->checkSameHashForKeys(
array_slice(($args = $this->getArguments()), 0, count($args) - 1)
@@ -7,6 +7,10 @@ class ListPopLastPushHead extends Command {
return 'RPOPLPUSH';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return $this->checkSameHashForKeys($this->getArguments());
}
@@ -7,6 +7,10 @@ class ListPopLastPushHeadBlocking extends Command {
return 'BRPOPLPUSH';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::skipLastArgument($arguments, $prefix);
}
protected function canBeHashed() {
return $this->checkSameHashForKeys(
array_slice($args = $this->getArguments(), 0, count($args) - 1)
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Predis\Commands;
class PrefixHelpers {
public static function multipleKeys(Array $arguments, $prefix) {
foreach ($arguments as &$key) {
$key = "$prefix$key";
}
return $arguments;
}
public static function skipLastArgument(Array $arguments, $prefix) {
$length = count($arguments);
for ($i = 0; $i < $length - 1; $i++) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
return $arguments;
}
}
@@ -1,7 +0,0 @@
<?php
namespace Predis\Commands\Preprocessors;
interface ICommandPreprocessor {
public function process($method, &$arguments);
}
@@ -1,11 +0,0 @@
<?php
namespace Predis\Commands\Preprocessors;
interface ICommandPreprocessorChain
extends ICommandPreprocessor, \IteratorAggregate, \Countable {
public function add(ICommandPreprocessor $preprocessor);
public function remove(ICommandPreprocessor $preprocessor);
public function getPreprocessors();
}
@@ -1,8 +0,0 @@
<?php
namespace Predis\Commands\Preprocessors;
interface IPreprocessingSupport {
public function setPreprocessor(ICommandPreprocessor $processor);
public function getPreprocessor();
}
@@ -1,181 +0,0 @@
<?php
namespace Predis\Commands\Preprocessors;
use Predis\ClientException;
use Predis\Profiles\IServerProfile;
class KeyPrefixPreprocessor implements ICommandPreprocessor {
private $_prefix;
private $_strategies;
public function __construct($prefix, IServerProfile $profile = null) {
if (isset($profile)) {
$this->checkProfile($profile);
}
$this->_prefix = $prefix;
$this->_strategies = $this->getPrefixStrategies();
}
protected function checkProfile(IServerProfile $profile) {
if (!in_array($profile, $this->getSupportedProfiles())) {
throw new ClientException("Unsupported profile: $profile");
}
}
protected function getSupportedProfiles() {
return array('1.2', '2.0', '2.2');
}
protected function getPrefixStrategies() {
$skipLast = function(&$arguments, $prefix) {
$length = count($arguments);
for ($i = 0; $i < $length - 1; $i++) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
};
$interleavedKeys = function(&$arguments, $prefix) {
$length = count($arguments);
if ($length === 1 && is_array($arguments[0])) {
$oldKvs = &$arguments[0];
$newKvs = array();
foreach ($oldKvs as $key => $value) {
$newKvs["$prefix$key"] = $value;
unset($oldKvs[$key]);
}
$arguments[0] = $newKvs;
}
else {
for ($i = 0; $i < $length; $i += 2) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
}
};
$zunionstore = function(&$arguments, $prefix) {
$arguments[0] = "$prefix{$arguments[0]}";
if (is_array($arguments[1])) {
foreach ($arguments[1] as &$destinationKey) {
$destinationKey = "$prefix$destinationKey";
}
$args = &$arguments[1];
$length = count($args);
for ($i = 0; $i < $length; $i++) {
$arguments[1][$i] = "$prefix{$args[$i]}";
}
}
else {
$length = (int)$arguments[1];
for ($i = 2; $i < $length; $i++) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
}
};
$sort = function(&$arguments, $prefix) {
$arguments[0] = "$prefix{$arguments[0]}";
if (count($arguments) === 1) {
return;
}
foreach ($arguments[1] as $modifier => &$value) {
switch (strtoupper($modifier)) {
case 'BY':
case 'STORE':
$value = "$prefix$value";
break;
case 'GET':
if (is_array($value)) {
foreach ($value as &$getItem) {
$getItem = "$prefix$getItem";
}
}
else {
$value = "$prefix$value";
}
break;
}
}
};
$debug = function(&$arguments, $prefix) {
if (count($arguments) === 3 && strtoupper($arguments[1]) == 'OBJECT') {
$arguments[2] = "$prefix{$arguments[2]}";
}
};
$cmdSingleKey = array(
'type', 'exists', 'move', 'expire', 'persist', 'sort', 'expireat', 'ttl', 'append',
'getrange', 'setnx', 'decr', 'getset', 'setrange', 'decrby', 'incr', 'set', 'strlen',
'get', 'incrby', 'setbit', 'getbit', 'setex', 'hdel', 'hgetall', 'hlen', 'hset',
'hexists', 'hincrby', 'hmget', 'hsetnx', 'hget', 'hkeys', 'hmset', 'hvals', 'lindex',
'linsert', 'llen', 'lpop', 'lpush', 'lpushx', 'rpushx', 'lrange', 'lrem', 'lset',
'ltrim', 'rpop', 'rpush', 'rpushx', 'sadd', 'scard', 'sismember', 'smembers', 'spop',
'srandmember', 'srem', 'zadd', 'zcard', 'zcount', 'zincrby', 'zrange', 'zrangebyscore',
'zrank', 'zrem', 'zremrangebyrank', 'zremrangebyscore', 'zrevrange', 'zrevrangebyscore',
'zrevrank', 'zscore', 'publish', 'keys',
);
$cmdMultiKeys = array(
'del', 'rename', 'renamenx', 'mget', 'rpoplpush', 'sdiff', 'sdiffstore', 'sinter',
'sinterstore', 'sunion', 'sunionstore', 'subscribe', 'punsubscribe', 'subscribe',
'unsubscribe', 'watch',
);
return array_merge(
array_fill_keys($cmdSingleKey, $this->getSingleKeyStrategy()),
array_fill_keys($cmdMultiKeys, $this->getMultipleKeysStrategy()),
array(
'blpop' => $skipLast, 'brpop' => $skipLast, 'brpoplpush' => $skipLast, 'smove' => $skipLast,
'mset' => $interleavedKeys, 'msetnx' => $interleavedKeys,
'zinterstore' => $zunionstore, 'zunionstore' => $zunionstore,
'sort' => $sort, 'debug' => $debug
)
);
}
public function setPrefixStrategy($command, $strategy) {
if (!is_callable($callable)) {
throw new \InvalidArgumentException(
'The command preprocessor strategy must be a callable object'
);
}
$this->_strategies[$command] = $strategy;
}
public function getSingleKeyStrategy() {
return function(&$arguments, $prefix) {
$arguments[0] = "$prefix{$arguments[0]}";
};
}
public function getMultipleKeysStrategy() {
return function(&$arguments, $prefix) {
if (count($arguments) === 1 && is_array($arguments[0])) {
$arguments = &$arguments[0];
}
foreach ($arguments as &$key) {
$key = "$prefix$key";
}
};
}
public function getPrefixStrategy($command) {
if (isset($this->_strategies[$command])) {
return $this->_strategies[$command];
}
}
public function setPrefix($prefix) {
$this->_prefix = $prefix;
}
public function getPrefix() {
return $this->_prefix;
}
public function process($method, &$arguments) {
if (isset($this->_strategies[$method])) {
$this->_strategies[$method]($arguments, $this->_prefix);
}
}
}
@@ -1,65 +0,0 @@
<?php
namespace Predis\Commands\Preprocessors;
class PreprocessorChain implements ICommandPreprocessorChain, \ArrayAccess {
private $_preprocessors;
public function __construct($preprocessors = array()) {
foreach ($preprocessors as $preprocessor) {
$this->add($preprocessor);
}
}
public function add(ICommandPreprocessor $preprocessor) {
$this->_preprocessors[] = $preprocessor;
}
public function remove(ICommandPreprocessor $preprocessor) {
$index = array_search($preprocessor, $this->_preprocessors, true);
if ($index !== false) {
unset($this->_preprocessors);
}
}
public function process($method, &$arguments) {
$count = count($this->_preprocessors);
for ($i = 0; $i < $count; $i++) {
$this->_preprocessors[$i]->process($method, $arguments);
}
}
public function getPreprocessors() {
return $this->_preprocessors;
}
public function getIterator() {
return new \ArrayIterator($this->_preprocessors);
}
public function count() {
return count($this->_preprocessors);
}
public function offsetExists($index) {
return isset($this->_preprocessors[$index]);
}
public function offsetGet($index) {
return $this->_preprocessors[$index];
}
public function offsetSet($index, $preprocessor) {
if (!$preprocessor instanceof ICommandPreprocessor) {
throw new \InvalidArgumentException(
'A preprocessor chain can hold only instances of classes implementing '.
'the Predis\Commands\Preprocessors\ICommandPreprocessor interface'
);
}
$this->_preprocessors[$index] = $preprocessor;
}
public function offsetUnset($index) {
unset($this->_preprocessors[$index]);
}
}
@@ -0,0 +1,9 @@
<?php
namespace Predis\Commands\Processors;
use Predis\Commands\ICommand;
interface ICommandProcessor {
public function process(ICommand $command);
}
@@ -0,0 +1,11 @@
<?php
namespace Predis\Commands\Processors;
interface ICommandProcessorChain
extends ICommandProcessor, \IteratorAggregate, \Countable {
public function add(ICommandProcessor $preprocessor);
public function remove(ICommandProcessor $preprocessor);
public function getProcessors();
}
@@ -0,0 +1,8 @@
<?php
namespace Predis\Commands\Processors;
interface IProcessingSupport {
public function setProcessor(ICommandProcessor $processor);
public function getProcessor();
}
@@ -0,0 +1,25 @@
<?php
namespace Predis\Commands\Processors;
use Predis\Commands\ICommand;
class KeyPrefixProcessor implements ICommandProcessor {
private $_prefix;
public function __construct($prefix) {
$this->setPrefix($prefix);
}
public function setPrefix($prefix) {
$this->_prefix = $prefix;
}
public function getPrefix() {
return $this->_prefix;
}
public function process(ICommand $command) {
$command->prefixKeys($this->_prefix);
}
}
@@ -0,0 +1,67 @@
<?php
namespace Predis\Commands\Processors;
use Predis\Commands\ICommand;
class ProcessorChain implements ICommandProcessorChain, \ArrayAccess {
private $_processors;
public function __construct($processors = array()) {
foreach ($processors as $processor) {
$this->add($processor);
}
}
public function add(ICommandProcessor $processor) {
$this->_processors[] = $processor;
}
public function remove(ICommandProcessor $processor) {
$index = array_search($processor, $this->_processors, true);
if ($index !== false) {
unset($this->_processors);
}
}
public function process(ICommand $command) {
$count = count($this->_processors);
for ($i = 0; $i < $count; $i++) {
$this->_processors[$i]->process($command);
}
}
public function getProcessors() {
return $this->_processors;
}
public function getIterator() {
return new \ArrayIterator($this->_processors);
}
public function count() {
return count($this->_processors);
}
public function offsetExists($index) {
return isset($this->_processors[$index]);
}
public function offsetGet($index) {
return $this->_processors[$index];
}
public function offsetSet($index, $processor) {
if (!$processor instanceof ICommandProcessor) {
throw new \InvalidArgumentException(
'A processor chain can hold only instances of classes implementing '.
'the Predis\Commands\Preprocessors\ICommandProcessor interface'
);
}
$this->_processors[$index] = $processor;
}
public function offsetUnset($index) {
unset($this->_processors[$index]);
}
}
+4
View File
@@ -13,6 +13,10 @@ class PubSubSubscribe extends Command {
return Helpers::filterArrayArguments($arguments);
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return false;
}
@@ -4,16 +4,8 @@ namespace Predis\Commands;
use Predis\Helpers;
class PubSubSubscribeByPattern extends Command {
class PubSubSubscribeByPattern extends PubSubSubscribe {
public function getId() {
return 'PSUBSCRIBE';
}
public function filterArguments(Array $arguments) {
return Helpers::filterArrayArguments($arguments);
}
protected function canBeHashed() {
return false;
}
}
@@ -7,6 +7,10 @@ class PubSubUnsubscribe extends Command {
return 'UNSUBSCRIBE';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return false;
}
@@ -2,12 +2,8 @@
namespace Predis\Commands;
class PubSubUnsubscribeByPattern extends Command {
class PubSubUnsubscribeByPattern extends PubSubUnsubscribe {
public function getId() {
return 'PUNSUBSCRIBE';
}
protected function canBeHashed() {
return false;
}
}
@@ -7,6 +7,10 @@ class ServerBackgroundRewriteAOF extends Command {
return 'BGREWRITEAOF';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
@@ -7,6 +7,10 @@ class ServerBackgroundSave extends Command {
return 'BGSAVE';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerClient extends Command {
return 'CLIENT';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerConfig extends Command {
return 'CONFIG';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
@@ -7,6 +7,10 @@ class ServerDatabaseSize extends Command {
return 'DBSIZE';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerFlushAll extends Command {
return 'FLUSHALL';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
@@ -7,6 +7,10 @@ class ServerFlushDatabase extends Command {
return 'FLUSHDB';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerInfo extends Command {
return 'INFO';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerLastSave extends Command {
return 'LASTSAVE';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerMonitor extends Command {
return 'MONITOR';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -9,6 +9,10 @@ class ServerObject extends Command {
return 'OBJECT';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerSave extends Command {
return 'SAVE';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class ServerShutdown extends Command {
return 'SHUTDOWN';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -14,6 +14,10 @@ class ServerSlaveOf extends Command {
return $arguments;
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -6,4 +6,8 @@ class SetDifference extends SetIntersection {
public function getId() {
return 'SDIFF';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
}
+4
View File
@@ -13,6 +13,10 @@ class SetIntersection extends Command {
return Helpers::filterArrayArguments($arguments);
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return $this->checkSameHashForKeys($this->getArguments());
}
@@ -14,6 +14,10 @@ class SetIntersectionStore extends Command {
return $arguments;
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return $this->checkSameHashForKeys($this->getArguments());
}
+4
View File
@@ -7,6 +7,10 @@ class SetMove extends Command {
return 'SMOVE';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::skipLastArgument($arguments, $prefix);
}
protected function canBeHashed() {
return false;
}
@@ -13,6 +13,10 @@ class StringGetMultiple extends Command {
return Helpers::filterArrayArguments($arguments);
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return $this->checkSameHashForKeys($this->getArguments());
}
@@ -20,6 +20,14 @@ class StringSetMultiple extends Command {
return $arguments;
}
protected function onPrefixKeys(Array $arguments, $prefix) {
$length = count($arguments);
for ($i = 0; $i < $length; $i += 2) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
return $arguments;
}
protected function canBeHashed() {
$args = $this->getArguments();
$keys = array();
@@ -7,6 +7,10 @@ class TransactionDiscard extends Command {
return 'DISCARD';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class TransactionExec extends Command {
return 'EXEC';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -7,6 +7,10 @@ class TransactionMulti extends Command {
return 'MULTI';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
@@ -7,6 +7,10 @@ class TransactionUnwatch extends Command {
return 'UNWATCH';
}
protected function onPrefixKeys(Array $arguments, $prefix) {
/* NOOP */
}
protected function canBeHashed() {
return false;
}
+4
View File
@@ -14,6 +14,10 @@ class TransactionWatch extends Command {
return $arguments;
}
protected function onPrefixKeys(Array $arguments, $prefix) {
return PrefixHelpers::multipleKeys($arguments, $prefix);
}
protected function canBeHashed() {
return false;
}
+9 -4
View File
@@ -5,8 +5,6 @@ namespace Predis\Commands;
use Predis\Iterators\MultiBulkResponseTuple;
class ZSetRange extends Command {
private $_withScores = false;
public function getId() {
return 'ZRANGE';
}
@@ -32,13 +30,20 @@ class ZSetRange extends Command {
$finalizedOpts = array();
if (isset($opts['WITHSCORES'])) {
$finalizedOpts[] = 'WITHSCORES';
$this->_withScores = true;
}
return $finalizedOpts;
}
protected function withScores() {
$arguments = $this->getArguments();
if (count($arguments) < 4) {
return false;
}
return strtoupper($arguments[3]) === 'WITHSCORES';
}
public function parseResponse($data) {
if ($this->_withScores) {
if ($this->withScores()) {
if ($data instanceof \Iterator) {
return new MultiBulkResponseTuple($data);
}
+14
View File
@@ -18,4 +18,18 @@ class ZSetRangeByScore extends ZSetRange {
}
return array_merge($finalizedOpts, parent::prepareOptions($options));
}
protected function withScores() {
$arguments = $this->getArguments();
for ($i = 3; $i < count($arguments); $i++) {
switch (strtoupper($arguments[$i])) {
case 'WITHSCORES':
return true;
case 'LIMIT':
$i += 2;
break;
}
}
return false;
}
}
+9
View File
@@ -38,6 +38,15 @@ class ZSetUnionStore extends Command {
return $finalizedOpts;
}
protected function onPrefixKeys(Array $arguments, $prefix) {
$arguments[0] = "$prefix{$arguments[0]}";
$length = ((int) $arguments[1]) + 2;
for ($i = 2; $i < $length; $i++) {
$arguments[$i] = "$prefix{$arguments[$i]}";
}
return $arguments;
}
protected function canBeHashed() {
$args = $this->getArguments();
return $this->checkSameHashForKeys(
+13 -13
View File
@@ -3,13 +3,13 @@
namespace Predis\Profiles;
use Predis\ClientException;
use Predis\Commands\Preprocessors\ICommandPreprocessor;
use Predis\Commands\Preprocessors\IPreprocessingSupport;
use Predis\Commands\Processors\ICommandProcessor;
use Predis\Commands\Processors\IProcessingSupport;
abstract class ServerProfile implements IServerProfile, IPreprocessingSupport {
abstract class ServerProfile implements IServerProfile, IProcessingSupport {
private static $_profiles;
private $_registeredCommands;
private $_preprocessor;
private $_processor;
public function __construct() {
$this->_registeredCommands = $this->getSupportedCommands();
@@ -76,12 +76,12 @@ abstract class ServerProfile implements IServerProfile, IPreprocessingSupport {
if (!isset($this->_registeredCommands[$method])) {
throw new ClientException("'$method' is not a registered Redis command");
}
if (isset($this->_preprocessor)) {
$this->_preprocessor->process($method, $arguments);
}
$commandClass = $this->_registeredCommands[$method];
$command = new $commandClass();
$command->setArguments($arguments);
if (isset($this->_processor)) {
$this->_processor->process($command);
}
return $command;
}
@@ -99,16 +99,16 @@ abstract class ServerProfile implements IServerProfile, IPreprocessingSupport {
$this->_registeredCommands[$alias] = $command;
}
public function setPreprocessor(ICommandPreprocessor $preprocessor) {
if (!isset($preprocessor)) {
unset($this->_preprocessor);
public function setProcessor(ICommandProcessor $processor) {
if (!isset($processor)) {
unset($this->_processor);
return;
}
$this->_preprocessor = $preprocessor;
$this->_processor = $processor;
}
public function getPreprocessor() {
return $this->_preprocessor;
public function getProcessor() {
return $this->_processor;
}
public function __toString() {