From be2ffa7d6c4e1ccb015f04b148e3f9548bcf1c33 Mon Sep 17 00:00:00 2001 From: Daniel Ceballos Date: Fri, 28 May 2010 04:26:37 +0800 Subject: [PATCH] adding redis cluster support --- lib/Resque.php | 15 ++++-- lib/Resque/RedisCluster.php | 101 ++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 lib/Resque/RedisCluster.php diff --git a/lib/Resque.php b/lib/Resque.php index 3a04f18..df91fc3 100644 --- a/lib/Resque.php +++ b/lib/Resque.php @@ -26,10 +26,17 @@ class Resque */ public static function setBackend($server) { - list($host, $port) = explode(':', $server); + if(is_array($server)) { - require_once dirname(__FILE__) . '/Resque/Redis.php'; - self::$redis = new Resque_Redis($host, $port); + require_once dirname(__FILE__) . '/Resque/RedisCluster.php'; + self::$redis = new Resque_RedisCluster($server); + + }else{ + list($host, $port) = explode(':', $server); + + require_once dirname(__FILE__) . '/Resque/Redis.php'; + self::$redis = new Resque_Redis($host, $port); + } } /** @@ -125,4 +132,4 @@ class Resque } return $queues; } -} \ No newline at end of file +} diff --git a/lib/Resque/RedisCluster.php b/lib/Resque/RedisCluster.php new file mode 100644 index 0000000..6ac15e3 --- /dev/null +++ b/lib/Resque/RedisCluster.php @@ -0,0 +1,101 @@ + + * @copyright (c) 2010 Chris Boulton + * @license http://www.opensource.org/licenses/mit-license.php + */ +class Resque_RedisCluster extends RedisentCluster +{ + /** + * @var array List of all commands in Redis that supply a key as their + * first argument. Used to prefix keys with the Resque namespace. + */ + private $keyCommands = array( + 'exists', + 'del', + 'type', + 'keys', + 'expire', + 'ttl', + 'move', + 'set', + 'get', + 'getset', + 'setnx', + 'incr', + 'incrby', + 'decrby', + 'decrby', + 'rpush', + 'lpush', + 'llen', + 'lrange', + 'ltrim', + 'lindex', + 'lset', + 'lrem', + 'lpop', + 'rpop', + 'sadd', + 'srem', + 'spop', + 'scard', + 'sismember', + 'smembers', + 'srandmember', + 'zadd', + 'zrem', + 'zrange', + 'zrevrange', + 'zrangebyscore', + 'zcard', + 'zscore', + 'zremrangebyscore', + 'sort' + ); + // sinterstore + // sunion + // sunionstore + // sdiff + // sdiffstore + // sinter + // smove + // rename + // rpoplpush + // mget + // msetnx + // mset + // renamenx + + /** + * Magic method to handle all function requests and prefix key based + * operations with the 'resque:' key prefix. + * + * @param string $name The name of the method called. + * @param array $args Array of supplied arguments to the method. + * @return mixed Return value from Resident::call() based on the command. + */ + public function __call($name, $args) { + $args = func_get_args(); + if(in_array($name, $this->keyCommands)) { + $args[1][0] = 'resque:' . $args[1][0]; + } + try { + return parent::__call($name, $args[1]); + } + catch(RedisException $e) { + return false; + } + } +} +?>