1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resque; |
4
|
|
|
|
5
|
|
|
use \Credis_Client; |
6
|
|
|
use \Credis_Cluster; |
7
|
|
|
use \CredisException; |
8
|
|
|
use \Resque\Exceptions\RedisException; |
9
|
|
|
use \InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Wrap Credis to add namespace support and various helper methods. |
13
|
|
|
* |
14
|
|
|
* @package Resque/Redis |
15
|
|
|
* @author Chris Boulton <[email protected]> |
16
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php |
17
|
|
|
*/ |
18
|
|
|
class Redis |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Redis namespace |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private static $defaultNamespace = 'resque:'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A default host to connect to |
28
|
|
|
*/ |
29
|
|
|
const DEFAULT_HOST = 'localhost'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The default Redis port |
33
|
|
|
*/ |
34
|
|
|
const DEFAULT_PORT = 6379; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The default Redis Database number |
38
|
|
|
*/ |
39
|
|
|
const DEFAULT_DATABASE = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Connection driver |
43
|
|
|
* @var mixed |
44
|
|
|
*/ |
45
|
|
|
private $driver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array List of all commands in Redis that supply a key as their |
49
|
|
|
* first argument. Used to prefix keys with the Resque namespace. |
50
|
|
|
*/ |
51
|
|
|
private $keyCommands = array( |
52
|
|
|
'exists', |
53
|
|
|
'del', |
54
|
|
|
'type', |
55
|
|
|
'keys', |
56
|
|
|
'expire', |
57
|
|
|
'ttl', |
58
|
|
|
'move', |
59
|
|
|
'set', |
60
|
|
|
'setex', |
61
|
|
|
'get', |
62
|
|
|
'getset', |
63
|
|
|
'setnx', |
64
|
|
|
'incr', |
65
|
|
|
'incrby', |
66
|
|
|
'decr', |
67
|
|
|
'decrby', |
68
|
|
|
'rpush', |
69
|
|
|
'lpush', |
70
|
|
|
'llen', |
71
|
|
|
'lrange', |
72
|
|
|
'ltrim', |
73
|
|
|
'lindex', |
74
|
|
|
'lset', |
75
|
|
|
'lrem', |
76
|
|
|
'lpop', |
77
|
|
|
'blpop', |
78
|
|
|
'rpop', |
79
|
|
|
'sadd', |
80
|
|
|
'srem', |
81
|
|
|
'spop', |
82
|
|
|
'scard', |
83
|
|
|
'sismember', |
84
|
|
|
'smembers', |
85
|
|
|
'srandmember', |
86
|
|
|
'zadd', |
87
|
|
|
'zrem', |
88
|
|
|
'zrange', |
89
|
|
|
'zrevrange', |
90
|
|
|
'zrangebyscore', |
91
|
|
|
'zcard', |
92
|
|
|
'zscore', |
93
|
|
|
'zremrangebyscore', |
94
|
|
|
'sort', |
95
|
|
|
'rename', |
96
|
|
|
'rpoplpush' |
97
|
|
|
); |
98
|
|
|
// sinterstore |
99
|
|
|
// sunion |
100
|
|
|
// sunionstore |
101
|
|
|
// sdiff |
102
|
|
|
// sdiffstore |
103
|
|
|
// sinter |
104
|
|
|
// smove |
105
|
|
|
// mget |
106
|
|
|
// msetnx |
107
|
|
|
// mset |
108
|
|
|
// renamenx |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set Redis namespace (prefix) default: resque |
112
|
|
|
* @param string $namespace |
113
|
|
|
*/ |
114
|
|
|
public static function prefix($namespace) |
115
|
|
|
{ |
116
|
|
|
if (substr($namespace, -1) !== ':' && $namespace != '') { |
117
|
|
|
$namespace .= ':'; |
118
|
|
|
} |
119
|
|
|
self::$defaultNamespace = $namespace; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|array $server A DSN or array |
124
|
|
|
* @param int $database A database number to select. However, if we find a valid database number in the DSN the |
125
|
|
|
* DSN-supplied value will be used instead and this parameter is ignored. |
126
|
|
|
* @param object $client Optional Credis_Cluster or Credis_Client instance instantiated by you |
127
|
|
|
*/ |
128
|
|
|
public function __construct($server, $database = null, $client = null) |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
|
|
if (is_object($client)) { |
132
|
|
|
$this->driver = $client; |
133
|
|
|
} elseif (is_object($server)) { |
|
|
|
|
134
|
|
|
$this->driver = $server; |
135
|
|
|
} elseif (is_array($server)) { |
136
|
|
|
$this->driver = new Credis_Cluster($server); |
|
|
|
|
137
|
|
|
} else { |
138
|
|
|
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); |
139
|
|
|
// $user is not used, only $password |
140
|
|
|
|
141
|
|
|
// Look for known Credis_Client options |
142
|
|
|
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null; |
143
|
|
|
$persistent = isset($options['persistent']) ? $options['persistent'] : ''; |
144
|
|
|
$maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0; |
145
|
|
|
|
146
|
|
|
$this->driver = new Credis_Client($host, $port, $timeout, $persistent); |
147
|
|
|
$this->driver->setMaxConnectRetries($maxRetries); |
148
|
|
|
if ($password) { |
149
|
|
|
$this->driver->auth($password); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// If we have found a database in our DSN, use it instead of the `$database` |
153
|
|
|
// value passed into the constructor. |
154
|
|
|
if ($dsnDatabase !== false) { |
155
|
|
|
$database = $dsnDatabase; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($database !== null) { |
160
|
|
|
$this->driver->select($database); |
161
|
|
|
} |
162
|
|
|
} catch (CredisException $e) { |
163
|
|
|
throw new RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Parse a DSN string, which can have one of the following formats: |
169
|
|
|
* |
170
|
|
|
* - host:port |
171
|
|
|
* - redis://user:pass@host:port/db?option1=val1&option2=val2 |
172
|
|
|
* - tcp://user:pass@host:port/db?option1=val1&option2=val2 |
173
|
|
|
* - unix:///path/to/redis.sock |
174
|
|
|
* |
175
|
|
|
* Note: the 'user' part of the DSN is not used. |
176
|
|
|
* |
177
|
|
|
* @param string $dsn A DSN string |
178
|
|
|
* @return array An array of DSN compotnents, with 'false' values for any unknown components. e.g. |
179
|
|
|
* [host, port, db, user, pass, options] |
180
|
|
|
*/ |
181
|
|
|
public static function parseDsn($dsn) |
182
|
|
|
{ |
183
|
|
|
if ($dsn == '') { |
184
|
|
|
// Use a sensible default for an empty DNS string |
185
|
|
|
$dsn = 'redis://' . self::DEFAULT_HOST; |
186
|
|
|
} |
187
|
|
|
if (substr($dsn, 0, 7) === 'unix://') { |
188
|
|
|
return array( |
189
|
|
|
$dsn, |
190
|
|
|
null, |
191
|
|
|
false, |
192
|
|
|
null, |
193
|
|
|
null, |
194
|
|
|
null, |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
$parts = parse_url($dsn); |
198
|
|
|
|
199
|
|
|
// Check the URI scheme |
200
|
|
|
$validSchemes = array('redis', 'tcp'); |
201
|
|
|
if (isset($parts['scheme']) && ! in_array($parts['scheme'], $validSchemes)) { |
202
|
|
|
throw new InvalidArgumentException("Invalid DSN. Supported schemes are " . implode(', ', $validSchemes)); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Allow simple 'hostname' format, which `parse_url` treats as a path, not host. |
206
|
|
|
if (! isset($parts['host']) && isset($parts['path'])) { |
207
|
|
|
$parts['host'] = $parts['path']; |
208
|
|
|
unset($parts['path']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// Extract the port number as an integer |
212
|
|
|
$port = isset($parts['port']) ? intval($parts['port']) : self::DEFAULT_PORT; |
213
|
|
|
|
214
|
|
|
// Get the database from the 'path' part of the URI |
215
|
|
|
$database = false; |
216
|
|
|
if (isset($parts['path'])) { |
217
|
|
|
// Strip non-digit chars from path |
218
|
|
|
$database = intval(preg_replace('/[^0-9]/', '', $parts['path'])); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Extract any 'user' values |
222
|
|
|
$user = isset($parts['user']) ? $parts['user'] : false; |
223
|
|
|
|
224
|
|
|
// Convert the query string into an associative array |
225
|
|
|
$options = array(); |
226
|
|
|
if (isset($parts['query'])) { |
227
|
|
|
// Parse the query string into an array |
228
|
|
|
parse_str($parts['query'], $options); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
//check 'password-encoding' parameter and extracting password based on encoding |
232
|
|
|
if ($options && isset($options['password-encoding']) && $options['password-encoding'] === 'u') { |
233
|
|
|
//extracting urlencoded password |
234
|
|
|
$pass = isset($parts['pass']) ? urldecode($parts['pass']) : false; |
235
|
|
|
} elseif ($options && isset($options['password-encoding']) && $options['password-encoding'] === 'b') { |
236
|
|
|
//extracting base64 encoded password |
237
|
|
|
$pass = isset($parts['pass']) ? base64_decode($parts['pass']) : false; |
238
|
|
|
} else { |
239
|
|
|
//extracting pass directly since 'password-encoding' parameter is not present |
240
|
|
|
$pass = isset($parts['pass']) ? $parts['pass'] : false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return array( |
244
|
|
|
$parts['host'], |
245
|
|
|
$port, |
246
|
|
|
$database, |
247
|
|
|
$user, |
248
|
|
|
$pass, |
249
|
|
|
$options, |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Magic method to handle all function requests and prefix key based |
255
|
|
|
* operations with the {self::$defaultNamespace} key prefix. |
256
|
|
|
* |
257
|
|
|
* @param string $name The name of the method called. |
258
|
|
|
* @param array $args Array of supplied arguments to the method. |
259
|
|
|
* @return mixed Return value from Resident::call() based on the command. |
260
|
|
|
*/ |
261
|
|
|
public function __call($name, $args) |
262
|
|
|
{ |
263
|
|
|
if (in_array($name, $this->keyCommands)) { |
264
|
|
|
if (is_array($args[0])) { |
265
|
|
|
foreach ($args[0] as $i => $v) { |
266
|
|
|
$args[0][$i] = self::$defaultNamespace . $v; |
267
|
|
|
} |
268
|
|
|
} else { |
269
|
|
|
$args[0] = self::$defaultNamespace . $args[0]; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
try { |
273
|
|
|
return $this->driver->__call($name, $args); |
274
|
|
|
} catch (CredisException $e) { |
275
|
|
|
throw new RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public static function getPrefix() |
280
|
|
|
{ |
281
|
|
|
return self::$defaultNamespace; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public static function removePrefix($string) |
285
|
|
|
{ |
286
|
|
|
$prefix = self::getPrefix(); |
287
|
|
|
|
288
|
|
|
if (substr($string, 0, strlen($prefix)) == $prefix) { |
289
|
|
|
$string = substr($string, strlen($prefix), strlen($string)); |
290
|
|
|
} |
291
|
|
|
return $string; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|