Added support of Redis prefix (namespaces)

This commit is contained in:
Hélio Costa e Silva 2011-11-22 14:55:40 -02:00
parent 4c025119cc
commit 016a7a13fd
3 changed files with 54 additions and 5 deletions

View file

@ -16,6 +16,11 @@ if(!class_exists('RedisentCluster')) {
*/
class Resque_RedisCluster extends RedisentCluster
{
/**
* Redis namespace
* @var string
*/
private static $defaultNamespace = 'resque:';
/**
* @var array List of all commands in Redis that supply a key as their
* first argument. Used to prefix keys with the Resque namespace.
@ -76,10 +81,22 @@ class Resque_RedisCluster extends RedisentCluster
// msetnx
// mset
// renamenx
/**
* Set Redis namespace (prefix) default: resque
* @param string $namespace
*/
public static function prefix($namespace)
{
if (strpos($namespace, ':') === false) {
$namespace .= ':';
}
self::$defaultNamespace = $namespace;
}
/**
* Magic method to handle all function requests and prefix key based
* operations with the 'resque:' key prefix.
* operations with the '{self::$defaultNamespace}' key prefix.
*
* @param string $name The name of the method called.
* @param array $args Array of supplied arguments to the method.
@ -88,7 +105,7 @@ class Resque_RedisCluster extends RedisentCluster
public function __call($name, $args) {
$args = func_get_args();
if(in_array($name, $this->keyCommands)) {
$args[1][0] = 'resque:' . $args[1][0];
$args[1][0] = self::$defaultNamespace . $args[1][0];
}
try {
return parent::__call($name, $args[1]);