diff --git a/examples/KeyPrefixes.php b/examples/KeyPrefixes.php new file mode 100644 index 00000000..fe614edb --- /dev/null +++ b/examples/KeyPrefixes.php @@ -0,0 +1,31 @@ +getProfile()->setPreprocessor(new KeyPrefixPreprocessor('nrk:')); + +$client->mset(array('foo' => 'bar', 'lol' => 'wut')); +var_dump($client->mget('foo', 'lol')); +/* +array(2) { + [0]=> string(3) "bar" + [1]=> string(3) "wut" +} +*/ + +var_dump($client->keys('*')); +/* +array(2) { + [0]=> string(7) "nrk:foo" + [1]=> string(7) "nrk:lol" +} +*/ +?> diff --git a/lib/Predis/Commands/Preprocessors/ICommandPreprocessor.php b/lib/Predis/Commands/Preprocessors/ICommandPreprocessor.php new file mode 100644 index 00000000..36bb4311 --- /dev/null +++ b/lib/Predis/Commands/Preprocessors/ICommandPreprocessor.php @@ -0,0 +1,7 @@ +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() { + $singleKey = function(&$arguments, $prefix) { + $arguments[0] = "$prefix{$arguments[0]}"; + }; + + $multiKeys = function(&$arguments, $prefix) { + if (count($arguments) === 1 && is_array($arguments[0])) { + $arguments = &$arguments[0]; + } + foreach ($arguments as &$key) { + $key = "$prefix$key"; + } + }; + + $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, $singleKey), + array_fill_keys($cmdMultiKeys, $multiKeys), + array( + 'blpop' => $skipLast, 'blpop' => $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 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); + } + } +} diff --git a/lib/Predis/Commands/Preprocessors/PreprocessorChain.php b/lib/Predis/Commands/Preprocessors/PreprocessorChain.php new file mode 100644 index 00000000..21fa1f27 --- /dev/null +++ b/lib/Predis/Commands/Preprocessors/PreprocessorChain.php @@ -0,0 +1,65 @@ +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]); + } +} diff --git a/lib/Predis/Profiles/ServerProfile.php b/lib/Predis/Profiles/ServerProfile.php index 128e4edd..fe6631db 100644 --- a/lib/Predis/Profiles/ServerProfile.php +++ b/lib/Predis/Profiles/ServerProfile.php @@ -3,10 +3,13 @@ namespace Predis\Profiles; use Predis\ClientException; +use Predis\Commands\Preprocessors\ICommandPreprocessor; +use Predis\Commands\Preprocessors\IPreprocessingSupport; -abstract class ServerProfile implements IServerProfile { +abstract class ServerProfile implements IServerProfile, IPreprocessingSupport { private static $_profiles; private $_registeredCommands; + private $_preprocessor; public function __construct() { $this->_registeredCommands = $this->getSupportedCommands(); @@ -73,6 +76,9 @@ abstract class ServerProfile implements IServerProfile { 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->setArgumentsArray($arguments); @@ -93,6 +99,18 @@ abstract class ServerProfile implements IServerProfile { $this->_registeredCommands[$alias] = $command; } + public function setPreprocessor(ICommandPreprocessor $preprocessor) { + if (!isset($preprocessor)) { + unset($this->_preprocessor); + return; + } + $this->_preprocessor = $preprocessor; + } + + public function getPreprocessor() { + return $this->_preprocessor; + } + public function __toString() { return $this->getVersion(); }