1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Redis usage |
13
|
|
|
* |
14
|
|
|
* <code> |
15
|
|
|
* // Get the default Redis database instance |
16
|
|
|
* $redis = Redis::db(); |
17
|
|
|
* |
18
|
|
|
* // Get a specified Redis database instance |
19
|
|
|
* $reids = Redis::db('redis_2'); |
20
|
|
|
* |
21
|
|
|
* // Execute the GET command for the "name" key |
22
|
|
|
* $name = Redis::db()->run('get', array('name')); |
23
|
|
|
* |
24
|
|
|
* // Execute the LRANGE command for the "list" key |
25
|
|
|
* $list = Redis::db()->run('lrange', array(0, 5)); |
26
|
|
|
* |
27
|
|
|
* </code> |
28
|
|
|
* |
29
|
|
|
* @package Pimf |
30
|
|
|
* @author Gjero Krsteski <[email protected]> |
31
|
|
|
* |
32
|
|
|
* @method expire($key, $seconds) |
33
|
|
|
* @method set($key, $value) |
34
|
|
|
* @method del($key) |
35
|
|
|
* @method forget($key) |
36
|
|
|
* @method get($key) |
37
|
|
|
* @method select($database_id) |
38
|
|
|
* @method put($session_id, $session, $lifetime); |
39
|
|
|
*/ |
40
|
|
|
class Redis |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Adapter\Socket |
45
|
|
|
*/ |
46
|
|
|
protected $socket; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The database number the connection selects on load. |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $database; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The connection to the Redis database. |
57
|
|
|
* |
58
|
|
|
* @var resource |
59
|
|
|
*/ |
60
|
|
|
protected $connection; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The active Redis database instances. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected static $databases = array(); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new Redis connection instance. |
71
|
|
|
* |
72
|
|
|
* @param Adapter\Socket $socket |
73
|
|
|
* @param int $database |
74
|
|
|
*/ |
75
|
|
|
public function __construct(Adapter\Socket $socket, $database = 0) |
76
|
|
|
{ |
77
|
|
|
$this->socket = $socket; |
78
|
|
|
$this->database = $database; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get a Redis database connection instance. |
83
|
|
|
* |
84
|
|
|
* The given name should correspond to a Redis database in the configuration file. |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* |
88
|
|
|
* @return Redis |
89
|
|
|
* @throws \RuntimeException |
90
|
|
|
*/ |
91
|
|
|
public static function database($name = 'default') |
92
|
|
|
{ |
93
|
|
|
if (!isset(static::$databases[$name])) { |
94
|
|
|
|
95
|
|
|
$cache = Config::get('cache'); |
96
|
|
|
|
97
|
|
|
if (!isset($cache['storage']) || $cache['storage'] != 'redis') { |
98
|
|
|
throw new \RuntimeException("Redis database [$name] is not defined."); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
static::$databases[$name] = new static( |
102
|
|
|
new Adapter\Socket($cache['server']['host'], $cache['server']['port']), |
103
|
|
|
$cache['server']['database'] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return static::$databases[$name]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Execute a command against the Redis database. |
112
|
|
|
* |
113
|
|
|
* @param string $method |
114
|
|
|
* @param array $parameters |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function run($method, $parameters) |
119
|
|
|
{ |
120
|
|
|
fwrite($this->connect(), $this->command($method, (array)$parameters)); |
121
|
|
|
|
122
|
|
|
$response = trim(fgets($this->connection, 512)); |
123
|
|
|
|
124
|
|
|
return $this->parse($response); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Parse and return the response from the Redis database. |
129
|
|
|
* |
130
|
|
|
* @param string $response |
131
|
|
|
* |
132
|
|
|
* @return array|string |
133
|
|
|
* @throws \RuntimeException |
134
|
|
|
*/ |
135
|
|
|
protected function parse($response) |
136
|
|
|
{ |
137
|
|
|
switch (substr($response, 0, 1)) { |
138
|
|
|
case '-': |
139
|
|
|
throw new \RuntimeException('Redis error: ' . substr(trim($response), 4)); |
140
|
|
|
|
141
|
|
|
case '+': |
142
|
|
|
case ':': |
143
|
|
|
return $this->inline($response); |
144
|
|
|
|
145
|
|
|
case '$': |
146
|
|
|
return $this->bulk($response); |
147
|
|
|
|
148
|
|
|
case '*': |
149
|
|
|
return $this->multibulk($response); |
150
|
|
|
|
151
|
|
|
default: |
152
|
|
|
throw new \RuntimeException("Unknown Redis response: " . substr($response, 0, 1)); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Establish the connection to the Redis database. |
158
|
|
|
* |
159
|
|
|
* @return resource |
160
|
|
|
*/ |
161
|
|
|
public function connect() |
162
|
|
|
{ |
163
|
|
|
if (!is_null($this->connection)) { |
164
|
|
|
return $this->connection; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->connection = $this->socket->open(); |
168
|
|
|
|
169
|
|
|
$this->select($this->database); |
170
|
|
|
|
171
|
|
|
return $this->connection; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Build the Redis command based from a given method and parameters. |
176
|
|
|
* |
177
|
|
|
* Redis protocol states that a command should conform to the following format: |
178
|
|
|
* |
179
|
|
|
* *<number of arguments> CR LF |
180
|
|
|
* $<number of bytes of argument 1> CR LF |
181
|
|
|
* <argument data> CR LF |
182
|
|
|
* ... |
183
|
|
|
* $<number of bytes of argument N> CR LF |
184
|
|
|
* <argument data> CR LF |
185
|
|
|
* |
186
|
|
|
* More information regarding the Redis protocol: http://redis.io/topics/protocol |
187
|
|
|
* |
188
|
|
|
* @param string $method |
189
|
|
|
* @param $parameters |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
protected function command($method, $parameters) |
194
|
|
|
{ |
195
|
|
|
$CRLF = "\r\n"; |
196
|
|
|
|
197
|
|
|
$command = '*' . (count($parameters) + 1) . $CRLF . '$' . strlen($method) . $CRLF . strtoupper($method) . $CRLF; |
198
|
|
|
|
199
|
|
|
foreach ($parameters as $parameter) { |
200
|
|
|
$command .= '$' . strlen($parameter) . $CRLF . $parameter . $CRLF; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $command; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Parse and handle an inline response from the Redis database. |
208
|
|
|
* |
209
|
|
|
* @param string $response |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
protected function inline($response) |
214
|
|
|
{ |
215
|
|
|
return substr(trim($response), 1); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Parse and handle a bulk response from the Redis database. |
220
|
|
|
* |
221
|
|
|
* @param string $head |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
protected function bulk($head) |
226
|
|
|
{ |
227
|
|
|
if ($head == '$-1') { |
228
|
|
|
return null; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
list($read, $response, $size) = array(0, '', substr($head, 1)); |
232
|
|
|
|
233
|
|
|
if ($size > 0) { |
234
|
|
|
do { |
235
|
|
|
|
236
|
|
|
// Calculate and read the appropriate bytes off of the Redis response. |
237
|
|
|
$block = (($remaining = $size - $read) < 1024) ? $remaining : 1024; |
238
|
|
|
$response .= fread($this->connection, $block); |
239
|
|
|
$read += $block; |
240
|
|
|
|
241
|
|
|
} while ($read < $size); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// The response ends with a trailing CRLF. |
245
|
|
|
fread($this->connection, 2); |
246
|
|
|
|
247
|
|
|
return $response; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Parse and handle a multi-bulk reply from the Redis database. |
252
|
|
|
* |
253
|
|
|
* @param string $head |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
protected function multibulk($head) |
258
|
|
|
{ |
259
|
|
|
if (($count = substr($head, 1)) == '-1') { |
260
|
|
|
return null; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$response = array(); |
264
|
|
|
|
265
|
|
|
// Iterate through each bulk response in the multi-bulk and parse it out. |
266
|
|
|
for ($i = 0; $i < $count; $i++) { |
267
|
|
|
$response[] = $this->parse(trim(fgets($this->connection, 512))); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $response; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Dynamically make calls to the Redis database. |
275
|
|
|
* |
276
|
|
|
* @param string $method |
277
|
|
|
* @param array $parameters |
278
|
|
|
* |
279
|
|
|
* @return mixed |
280
|
|
|
*/ |
281
|
|
|
public function __call($method, $parameters) |
282
|
|
|
{ |
283
|
|
|
return $this->run($method, $parameters); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Dynamically pass static method calls to the Redis instance. |
288
|
|
|
* |
289
|
|
|
* @param $method |
290
|
|
|
* @param $parameters |
291
|
|
|
* |
292
|
|
|
* @return mixed |
293
|
|
|
*/ |
294
|
|
|
public static function __callStatic($method, $parameters) |
295
|
|
|
{ |
296
|
|
|
return static::database()->run($method, $parameters); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Close the connection to the Redis database. |
301
|
|
|
* |
302
|
|
|
* @return void |
303
|
|
|
*/ |
304
|
|
|
public function __destruct() |
305
|
|
|
{ |
306
|
|
|
if ($this->connection) { |
307
|
|
|
fclose($this->connection); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|