Skip to content

Commit

Permalink
Added support of Redis prefix (namespaces)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hélio Costa e Silva committed Nov 22, 2011
1 parent 4c02511 commit 016a7a1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
23 changes: 20 additions & 3 deletions lib/Resque/Redis.php
Expand Up @@ -16,6 +16,11 @@
*/
class Resque_Redis extends Redisent
{
/**
* 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.
Expand Down Expand Up @@ -76,10 +81,22 @@ class Resque_Redis extends Redisent
// 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.
Expand All @@ -88,7 +105,7 @@ class Resque_Redis extends Redisent
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]);
Expand Down
21 changes: 19 additions & 2 deletions lib/Resque/RedisCluster.php
Expand Up @@ -16,6 +16,11 @@
*/
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.
Expand Down Expand Up @@ -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.
Expand All @@ -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]);
Expand Down
15 changes: 15 additions & 0 deletions test/Resque/Tests/JobTest.php
Expand Up @@ -100,6 +100,7 @@ public function testRecreatedJobMatchesExistingJob()
$this->assertEquals($job->payload['args'], $newJob->getArguments());
}


public function testFailedJobExceptionsAreCaught()
{
$payload = array(
Expand Down Expand Up @@ -166,4 +167,18 @@ public function testJobWithTearDownCallbackFiresTearDown()

$this->assertTrue(Test_Job_With_TearDown::$called);
}

public function testJobWithNamespace()
{
Resque_Redis::prefix('php');
$queue = 'jobs';
$payload = array('another_value');
Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload);

$this->assertEquals(Resque::queues(), array('jobs'));
$this->assertEquals(Resque::size($queue), 1);

Resque_Redis::prefix('resque');
$this->assertEquals(Resque::size($queue), 0);
}
}

0 comments on commit 016a7a1

Please sign in to comment.