This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
/
Redis.php
executable file
·183 lines (167 loc) · 4.85 KB
/
Redis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
if (class_exists('Redis')) {
class RedisApi extends Redis
{
private static $defaultNamespace = 'resque:';
public function __construct($host, $port, $timeout = 5, $password = null)
{
parent::__construct();
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->password = $password;
$this->establishConnection();
}
function establishConnection()
{
$this->pconnect($this->host, (int)$this->port, (int)$this->timeout, getmypid());
if ($this->password !== null) {
$this->auth($this->password);
}
$this->setOption(Redis::OPT_PREFIX, self::$defaultNamespace);
}
public function prefix($namespace)
{
if (empty($namespace)) $namespace = self::$defaultNamespace;
if (strpos($namespace, ':') === false) {
$namespace .= ':';
}
self::$defaultNamespace = $namespace;
$this->setOption(Redis::OPT_PREFIX, self::$defaultNamespace);
}
public static function getPrefix()
{
return '';
}
}
} else {
// 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.
*
* @package Resque/Redis
* @author Chris Boulton <chris.boulton@interspire.com>
* @copyright (c) 2010 Chris Boulton
* @license http://www.opensource.org/licenses/mit-license.php
*/
class RedisApi extends Redisent
{
/**
* Redis namespace
* @var string
*/
private static $defaultNamespace = 'resque:';
/**
* @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 = [
'exists',
'del',
'type',
'keys',
'expire',
'ttl',
'move',
'set',
'setex',
'get',
'getset',
'setnx',
'incr',
'incrby',
'decr',
'decrby',
'rpush',
'lpush',
'llen',
'lrange',
'ltrim',
'lindex',
'lset',
'lrem',
'lpop',
'rpop',
'sadd',
'srem',
'spop',
'scard',
'sismember',
'smembers',
'srandmember',
'zadd',
'zrem',
'zrange',
'zrevrange',
'zrangebyscore',
'zcard',
'zscore',
'zremrangebyscore',
'sort',
'rpoplpush',
];
// sinterstore
// sunion
// sunionstore
// sdiff
// sdiffstore
// sinter
// smove
// rename
// mget
// msetnx
// mset
// renamenx
/**
* Set Redis namespace (prefix) default: resque
* @param string $namespace
*/
public function prefix($namespace)
{
if (strpos($namespace, ':') === false) {
$namespace .= ':';
}
self::$defaultNamespace = $namespace;
}
/**
* Magic method to handle all function requests and prefix key based
* operations with the {self::$defaultNamespace} key prefix.
*
* @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)
{
$args = func_get_args();
if (in_array(strtolower($name), $this->keyCommands)) {
$args[1][0] = self::$defaultNamespace . $args[1][0];
}
try {
return parent::__call($name, $args[1]);
} catch (RedisException $e) {
return false;
}
}
public static function getPrefix()
{
return self::$defaultNamespace;
}
}
}
class Resque_Redis extends redisApi
{
public function __construct($host, $port, $password = null)
{
if (is_subclass_of($this, 'Redis')) {
parent::__construct($host, $port, 5, $password);
} else {
parent::__construct($host, $port);
}
}
}