mirror of
https://github.com/idanoo/php-resque
synced 2025-07-01 13:42:22 +00:00
Merge branch 'master' of git://github.com/chrisboulton/php-resque into blocking-list-pop
Conflicts: lib/Resque.php lib/Resque/RedisCluster.php lib/Resque/Worker.php
This commit is contained in:
commit
5687c8fe82
41 changed files with 455 additions and 767 deletions
|
@ -1,26 +1,22 @@
|
|||
<?php
|
||||
// Third- party apps may have already loaded Resident from elsewhere
|
||||
// so lets be careful.
|
||||
if(!class_exists('Redisent', false)) {
|
||||
require_once dirname(__FILE__) . '/../Redisent/Redisent.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended Redisent class used by Resque for all communication with
|
||||
* redis. Essentially adds namespace support to Redisent.
|
||||
* Wrap Credis to add namespace support and various helper methods.
|
||||
*
|
||||
* @package Resque/Redis
|
||||
* @author Chris Boulton <chris.boulton@interspire.com>
|
||||
* @copyright (c) 2010 Chris Boulton
|
||||
* @author Chris Boulton <chris@bigcommerce.com>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php
|
||||
*/
|
||||
class Resque_Redis extends Redisent
|
||||
class Resque_Redis
|
||||
{
|
||||
/**
|
||||
* Redis namespace
|
||||
* @var string
|
||||
*/
|
||||
private static $defaultNamespace = 'resque:';
|
||||
|
||||
private $server;
|
||||
private $database;
|
||||
|
||||
/**
|
||||
* @var array List of all commands in Redis that supply a key as their
|
||||
* first argument. Used to prefix keys with the Resque namespace.
|
||||
|
@ -34,6 +30,7 @@ class Resque_Redis extends Redisent
|
|||
'ttl',
|
||||
'move',
|
||||
'set',
|
||||
'setex',
|
||||
'get',
|
||||
'getset',
|
||||
'setnx',
|
||||
|
@ -82,7 +79,7 @@ class Resque_Redis extends Redisent
|
|||
// msetnx
|
||||
// mset
|
||||
// renamenx
|
||||
|
||||
|
||||
/**
|
||||
* Set Redis namespace (prefix) default: resque
|
||||
* @param string $namespace
|
||||
|
@ -94,7 +91,48 @@ class Resque_Redis extends Redisent
|
|||
}
|
||||
self::$defaultNamespace = $namespace;
|
||||
}
|
||||
|
||||
|
||||
public function __construct($server, $database = null)
|
||||
{
|
||||
$this->server = $server;
|
||||
$this->database = $database;
|
||||
|
||||
if (is_array($this->server)) {
|
||||
$this->driver = new Credis_Cluster($server);
|
||||
}
|
||||
else {
|
||||
$port = null;
|
||||
$password = null;
|
||||
$host = $server;
|
||||
|
||||
// If not a UNIX socket path or tcp:// formatted connections string
|
||||
// assume host:port combination.
|
||||
if (strpos($server, '/') === false) {
|
||||
$parts = explode(':', $server);
|
||||
if (isset($parts[1])) {
|
||||
$port = $parts[1];
|
||||
}
|
||||
$host = $parts[0];
|
||||
}else if (strpos($server, 'redis://') !== false){
|
||||
// Redis format is:
|
||||
// redis://[user]:[password]@[host]:[port]
|
||||
list($userpwd,$hostport) = explode('@', $server);
|
||||
$userpwd = substr($userpwd, strpos($userpwd, 'redis://')+8);
|
||||
list($host, $port) = explode(':', $hostport);
|
||||
list($user, $password) = explode(':', $userpwd);
|
||||
}
|
||||
|
||||
$this->driver = new Credis_Client($host, $port);
|
||||
if (isset($password)){
|
||||
$this->driver->auth($password);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->database !== null) {
|
||||
$this->driver->select($database);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to handle all function requests and prefix key based
|
||||
* operations with the {self::$defaultNamespace} key prefix.
|
||||
|
@ -104,16 +142,30 @@ class Resque_Redis extends Redisent
|
|||
* @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] = self::$defaultNamespace . $args[1][0];
|
||||
$args[0] = self::$defaultNamespace . $args[0];
|
||||
}
|
||||
try {
|
||||
return parent::__call($name, $args[1]);
|
||||
return $this->driver->__call($name, $args);
|
||||
}
|
||||
catch(RedisException $e) {
|
||||
catch(CredisException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getPrefix()
|
||||
{
|
||||
return self::$defaultNamespace;
|
||||
}
|
||||
|
||||
public static function removePrefix($string)
|
||||
{
|
||||
$prefix=self::getPrefix();
|
||||
|
||||
if (substr($string, 0, strlen($prefix)) == $prefix) {
|
||||
$string = substr($string, strlen($prefix), strlen($string) );
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue