Skip to content

Commit 45c49cf

Browse files
author
Chris Boulton
committed
Merge pull request #27 from hlegius/master
Namespace support (prefix)
2 parents 4c02511 + 016a7a1 commit 45c49cf

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

lib/Resque/Redis.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
*/
1717
class Resque_Redis extends Redisent
1818
{
19+
/**
20+
* Redis namespace
21+
* @var string
22+
*/
23+
private static $defaultNamespace = 'resque:';
1924
/**
2025
* @var array List of all commands in Redis that supply a key as their
2126
* first argument. Used to prefix keys with the Resque namespace.
@@ -76,10 +81,22 @@ class Resque_Redis extends Redisent
7681
// msetnx
7782
// mset
7883
// renamenx
79-
84+
85+
/**
86+
* Set Redis namespace (prefix) default: resque
87+
* @param string $namespace
88+
*/
89+
public static function prefix($namespace)
90+
{
91+
if (strpos($namespace, ':') === false) {
92+
$namespace .= ':';
93+
}
94+
self::$defaultNamespace = $namespace;
95+
}
96+
8097
/**
8198
* Magic method to handle all function requests and prefix key based
82-
* operations with the 'resque:' key prefix.
99+
* operations with the {self::$defaultNamespace} key prefix.
83100
*
84101
* @param string $name The name of the method called.
85102
* @param array $args Array of supplied arguments to the method.
@@ -88,7 +105,7 @@ class Resque_Redis extends Redisent
88105
public function __call($name, $args) {
89106
$args = func_get_args();
90107
if(in_array($name, $this->keyCommands)) {
91-
$args[1][0] = 'resque:' . $args[1][0];
108+
$args[1][0] = self::$defaultNamespace . $args[1][0];
92109
}
93110
try {
94111
return parent::__call($name, $args[1]);

lib/Resque/RedisCluster.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
*/
1717
class Resque_RedisCluster extends RedisentCluster
1818
{
19+
/**
20+
* Redis namespace
21+
* @var string
22+
*/
23+
private static $defaultNamespace = 'resque:';
1924
/**
2025
* @var array List of all commands in Redis that supply a key as their
2126
* first argument. Used to prefix keys with the Resque namespace.
@@ -76,10 +81,22 @@ class Resque_RedisCluster extends RedisentCluster
7681
// msetnx
7782
// mset
7883
// renamenx
84+
85+
/**
86+
* Set Redis namespace (prefix) default: resque
87+
* @param string $namespace
88+
*/
89+
public static function prefix($namespace)
90+
{
91+
if (strpos($namespace, ':') === false) {
92+
$namespace .= ':';
93+
}
94+
self::$defaultNamespace = $namespace;
95+
}
7996

8097
/**
8198
* Magic method to handle all function requests and prefix key based
82-
* operations with the 'resque:' key prefix.
99+
* operations with the '{self::$defaultNamespace}' key prefix.
83100
*
84101
* @param string $name The name of the method called.
85102
* @param array $args Array of supplied arguments to the method.
@@ -88,7 +105,7 @@ class Resque_RedisCluster extends RedisentCluster
88105
public function __call($name, $args) {
89106
$args = func_get_args();
90107
if(in_array($name, $this->keyCommands)) {
91-
$args[1][0] = 'resque:' . $args[1][0];
108+
$args[1][0] = self::$defaultNamespace . $args[1][0];
92109
}
93110
try {
94111
return parent::__call($name, $args[1]);

test/Resque/Tests/JobTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function testRecreatedJobMatchesExistingJob()
100100
$this->assertEquals($job->payload['args'], $newJob->getArguments());
101101
}
102102

103+
103104
public function testFailedJobExceptionsAreCaught()
104105
{
105106
$payload = array(
@@ -166,4 +167,18 @@ public function testJobWithTearDownCallbackFiresTearDown()
166167

167168
$this->assertTrue(Test_Job_With_TearDown::$called);
168169
}
170+
171+
public function testJobWithNamespace()
172+
{
173+
Resque_Redis::prefix('php');
174+
$queue = 'jobs';
175+
$payload = array('another_value');
176+
Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload);
177+
178+
$this->assertEquals(Resque::queues(), array('jobs'));
179+
$this->assertEquals(Resque::size($queue), 1);
180+
181+
Resque_Redis::prefix('resque');
182+
$this->assertEquals(Resque::size($queue), 0);
183+
}
169184
}

0 commit comments

Comments
 (0)