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
|
|
|
{
|
2014-05-05 13:02:16 +00:00
|
|
|
/**
|
|
|
|
* Redis namespace
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $defaultNamespace = 'resque:';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A default host to connect to
|
|
|
|
*/
|
|
|
|
const DEFAULT_HOST = 'localhost';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default Redis port
|
|
|
|
*/
|
|
|
|
const DEFAULT_PORT = 6379;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default Redis Database number
|
|
|
|
*/
|
|
|
|
const DEFAULT_DATABASE = 0;
|
2013-01-12 12:37:38 +00:00
|
|
|
|
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',
|
2014-09-19 11:29:53 +00:00
|
|
|
'sort',
|
2014-09-21 08:21:08 +00:00
|
|
|
'rename',
|
2014-09-17 00:20:26 +00:00
|
|
|
'rpoplpush'
|
2010-04-18 13:58:43 +00:00
|
|
|
);
|
|
|
|
// sinterstore
|
|
|
|
// sunion
|
|
|
|
// sunionstore
|
|
|
|
// sdiff
|
|
|
|
// sdiffstore
|
|
|
|
// sinter
|
|
|
|
// smove
|
|
|
|
// 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)
|
|
|
|
{
|
2015-03-11 22:41:12 +00:00
|
|
|
if (substr($namespace, -1) !== ':') {
|
2011-11-22 16:55:40 +00:00
|
|
|
$namespace .= ':';
|
|
|
|
}
|
|
|
|
self::$defaultNamespace = $namespace;
|
|
|
|
}
|
2013-01-12 12:37:38 +00:00
|
|
|
|
2014-05-05 13:02:16 +00:00
|
|
|
/**
|
|
|
|
* @param string|array $server A DSN or array
|
2014-05-05 14:55:01 +00:00
|
|
|
* @param int $database A database number to select. However, if we find a valid database number in the DSN the
|
|
|
|
* DSN-supplied value will be used instead and this parameter is ignored.
|
2014-05-05 13:02:16 +00:00
|
|
|
*/
|
|
|
|
public function __construct($server, $database = null)
|
2013-01-12 12:37:38 +00:00
|
|
|
{
|
2014-05-05 15:08:00 +00:00
|
|
|
if (is_array($server)) {
|
2013-01-12 12:37:38 +00:00
|
|
|
$this->driver = new Credis_Cluster($server);
|
2014-05-05 14:59:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-05-05 13:02:16 +00:00
|
|
|
|
2014-05-05 15:08:00 +00:00
|
|
|
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
|
2014-05-05 14:47:43 +00:00
|
|
|
// $user is not used, only $password
|
2014-05-05 13:02:16 +00:00
|
|
|
|
|
|
|
// Look for known Credis_Client options
|
|
|
|
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
|
|
|
|
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
|
2016-01-26 13:54:02 +00:00
|
|
|
$maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0;
|
2014-05-05 13:02:16 +00:00
|
|
|
|
|
|
|
$this->driver = new Credis_Client($host, $port, $timeout, $persistent);
|
2016-01-26 13:54:02 +00:00
|
|
|
$this->driver->setMaxConnectRetries($maxRetries);
|
2014-05-05 13:02:16 +00:00
|
|
|
if ($password){
|
2013-02-20 16:41:41 +00:00
|
|
|
$this->driver->auth($password);
|
|
|
|
}
|
2014-05-05 13:02:16 +00:00
|
|
|
|
2014-05-05 14:47:43 +00:00
|
|
|
// If we have found a database in our DSN, use it instead of the `$database`
|
2014-05-05 14:55:01 +00:00
|
|
|
// value passed into the constructor.
|
2014-05-05 14:47:43 +00:00
|
|
|
if ($dsnDatabase !== false) {
|
2014-05-05 13:02:16 +00:00
|
|
|
$database = $dsnDatabase;
|
|
|
|
}
|
2013-01-12 12:37:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 15:08:00 +00:00
|
|
|
if ($database !== null) {
|
2013-01-12 12:37:38 +00:00
|
|
|
$this->driver->select($database);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 13:02:16 +00:00
|
|
|
/**
|
2014-05-05 14:47:43 +00:00
|
|
|
* Parse a DSN string, which can have one of the following formats:
|
|
|
|
*
|
|
|
|
* - host:port
|
|
|
|
* - redis://user:pass@host:port/db?option1=val1&option2=val2
|
|
|
|
* - tcp://user:pass@host:port/db?option1=val1&option2=val2
|
2016-08-15 13:33:18 +00:00
|
|
|
* - unix:///path/to/redis.sock
|
2014-05-05 14:47:43 +00:00
|
|
|
*
|
|
|
|
* Note: the 'user' part of the DSN is not used.
|
|
|
|
*
|
|
|
|
* @param string $dsn A DSN string
|
2014-05-05 15:08:00 +00:00
|
|
|
* @return array An array of DSN compotnents, with 'false' values for any unknown components. e.g.
|
|
|
|
* [host, port, db, user, pass, options]
|
2014-05-05 13:02:16 +00:00
|
|
|
*/
|
2014-05-05 15:08:00 +00:00
|
|
|
public static function parseDsn($dsn)
|
2014-05-05 13:02:16 +00:00
|
|
|
{
|
|
|
|
if ($dsn == '') {
|
|
|
|
// Use a sensible default for an empty DNS string
|
|
|
|
$dsn = 'redis://' . self::DEFAULT_HOST;
|
|
|
|
}
|
2016-08-15 13:33:18 +00:00
|
|
|
if(substr($dsn, 0, 7) === 'unix://') {
|
|
|
|
return array(
|
|
|
|
$dsn,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
}
|
2014-05-05 13:02:16 +00:00
|
|
|
$parts = parse_url($dsn);
|
2014-05-05 14:47:43 +00:00
|
|
|
|
|
|
|
// Check the URI scheme
|
|
|
|
$validSchemes = array('redis', 'tcp');
|
2014-05-05 13:02:16 +00:00
|
|
|
if (isset($parts['scheme']) && ! in_array($parts['scheme'], $validSchemes)) {
|
|
|
|
throw new \InvalidArgumentException("Invalid DSN. Supported schemes are " . implode(', ', $validSchemes));
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:47:43 +00:00
|
|
|
// Allow simple 'hostname' format, which `parse_url` treats as a path, not host.
|
|
|
|
if ( ! isset($parts['host']) && isset($parts['path'])) {
|
|
|
|
$parts['host'] = $parts['path'];
|
|
|
|
unset($parts['path']);
|
2014-05-05 13:02:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 14:47:43 +00:00
|
|
|
// Extract the port number as an integer
|
2014-05-05 13:02:16 +00:00
|
|
|
$port = isset($parts['port']) ? intval($parts['port']) : self::DEFAULT_PORT;
|
|
|
|
|
2014-05-05 14:47:43 +00:00
|
|
|
// Get the database from the 'path' part of the URI
|
|
|
|
$database = false;
|
2014-05-05 13:02:16 +00:00
|
|
|
if (isset($parts['path'])) {
|
|
|
|
// Strip non-digit chars from path
|
|
|
|
$database = intval(preg_replace('/[^0-9]/', '', $parts['path']));
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:47:43 +00:00
|
|
|
// Extract any 'user' and 'pass' values
|
|
|
|
$user = isset($parts['user']) ? $parts['user'] : false;
|
|
|
|
$pass = isset($parts['pass']) ? $parts['pass'] : false;
|
|
|
|
|
|
|
|
// Convert the query string into an associative array
|
2014-05-05 13:02:16 +00:00
|
|
|
$options = array();
|
|
|
|
if (isset($parts['query'])) {
|
|
|
|
// Parse the query string into an array
|
|
|
|
parse_str($parts['query'], $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$parts['host'],
|
|
|
|
$port,
|
|
|
|
$database,
|
2014-05-05 14:47:43 +00:00
|
|
|
$user,
|
|
|
|
$pass,
|
2014-05-05 13:02:16 +00:00
|
|
|
$options,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-05-05 15:08:00 +00:00
|
|
|
public function __call($name, $args)
|
|
|
|
{
|
|
|
|
if (in_array($name, $this->keyCommands)) {
|
|
|
|
if (is_array($args[0])) {
|
|
|
|
foreach ($args[0] AS $i => $v) {
|
|
|
|
$args[0][$i] = self::$defaultNamespace . $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$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
|
|
|
}
|
2014-05-05 15:08:00 +00:00
|
|
|
catch (CredisException $e) {
|
2010-04-18 13:58:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 15:23:57 +00:00
|
|
|
|
2014-05-05 15:08:00 +00:00
|
|
|
public static function getPrefix()
|
|
|
|
{
|
|
|
|
return self::$defaultNamespace;
|
|
|
|
}
|
2012-11-26 15:23:57 +00:00
|
|
|
|
2014-05-05 15:08:00 +00:00
|
|
|
public static function removePrefix($string)
|
|
|
|
{
|
|
|
|
$prefix=self::getPrefix();
|
2012-11-26 15:23:57 +00:00
|
|
|
|
2014-05-05 15:08:00 +00:00
|
|
|
if (substr($string, 0, strlen($prefix)) == $prefix) {
|
|
|
|
$string = substr($string, strlen($prefix), strlen($string) );
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|