2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-01-12 12:37:38 +00:00
|
|
|
* Wrap Credis to add namespace support and various helper methods.
|
2010-04-18 13:58:43 +00:00
|
|
|
*
|
|
|
|
* @package Resque/Redis
|
2012-10-13 06:55:41 +00:00
|
|
|
* @author Chris Boulton <chris@bigcommerce.com>
|
2010-04-18 13:58:43 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
2013-01-12 12:37:38 +00:00
|
|
|
class Resque_Redis
|
2010-04-18 13:58:43 +00:00
|
|
|
{
|
2011-11-22 16:55:40 +00:00
|
|
|
/**
|
|
|
|
* Redis namespace
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $defaultNamespace = 'resque:';
|
2013-01-12 12:37:38 +00:00
|
|
|
|
|
|
|
private $server;
|
|
|
|
private $database;
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
|
|
|
* @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',
|
2013-01-11 20:40:39 +00:00
|
|
|
'setex',
|
2010-04-18 13:58:43 +00:00
|
|
|
'get',
|
|
|
|
'getset',
|
|
|
|
'setnx',
|
|
|
|
'incr',
|
|
|
|
'incrby',
|
2012-02-18 16:10:06 +00:00
|
|
|
'decr',
|
2010-04-18 13:58:43 +00:00
|
|
|
'decrby',
|
|
|
|
'rpush',
|
|
|
|
'lpush',
|
|
|
|
'llen',
|
|
|
|
'lrange',
|
|
|
|
'ltrim',
|
|
|
|
'lindex',
|
|
|
|
'lset',
|
|
|
|
'lrem',
|
|
|
|
'lpop',
|
2012-08-24 14:51:03 +00:00
|
|
|
'blpop',
|
2010-04-18 13:58:43 +00:00
|
|
|
'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
|
2013-01-12 12:37:38 +00:00
|
|
|
|
2011-11-22 16:55:40 +00:00
|
|
|
/**
|
|
|
|
* Set Redis namespace (prefix) default: resque
|
|
|
|
* @param string $namespace
|
|
|
|
*/
|
|
|
|
public static function prefix($namespace)
|
|
|
|
{
|
|
|
|
if (strpos($namespace, ':') === false) {
|
|
|
|
$namespace .= ':';
|
|
|
|
}
|
|
|
|
self::$defaultNamespace = $namespace;
|
|
|
|
}
|
2013-01-12 12:37:38 +00:00
|
|
|
|
|
|
|
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;
|
2013-02-20 16:41:41 +00:00
|
|
|
$password = null;
|
2013-01-12 12:37:38 +00:00
|
|
|
$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];
|
2013-02-20 16:41:41 +00:00
|
|
|
}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);
|
2013-01-12 12:37:38 +00:00
|
|
|
}
|
2013-02-20 16:41:41 +00:00
|
|
|
|
2013-01-12 12:37:38 +00:00
|
|
|
$this->driver = new Credis_Client($host, $port);
|
2013-02-20 16:41:41 +00:00
|
|
|
if (isset($password)){
|
|
|
|
$this->driver->auth($password);
|
|
|
|
}
|
2013-01-12 12:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->database !== null) {
|
|
|
|
$this->driver->select($database);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
|
|
|
* Magic method to handle all function requests and prefix key based
|
2011-11-22 16:55:40 +00:00
|
|
|
* operations with the {self::$defaultNamespace} key prefix.
|
2010-04-18 13:58:43 +00:00
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
if(in_array($name, $this->keyCommands)) {
|
2013-01-12 12:37:38 +00:00
|
|
|
$args[0] = self::$defaultNamespace . $args[0];
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
try {
|
2013-01-12 12:37:38 +00:00
|
|
|
return $this->driver->__call($name, $args);
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
2013-01-12 12:37:38 +00:00
|
|
|
catch(CredisException $e) {
|
2010-04-18 13:58:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 15:23:57 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
?>
|