My favorites
▼
|
Sign in
imemcacheclient-php
Improved Memcache client (for PHP).
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
Redis.class.php
‹r132
r141
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/*
@class Redis
@package IMemcacheClient
@url http://code.google.com/p/imemcacheclient-php/
@author kak.serpom.po.yaitsam@gmail.com
@description Connector for Redis (http://code.google.com/p/redis/)
@license LGPL, BSD-compabible. Adding to the Redis repository permitted.
*/
class Redis
{
public $servers = array();
public $default_port = 6379;
public $dtags_enabled = TRUE;
public $pool = array();
public function __construct() {}
public function addServer($host,$port = NULL,$weight = NULL)
{
if ($port === NULL) {$port = $this->default_port;}
$this->servers[$host.':'.$port] = $weight;
}
private function getConnection($addr)
{
if (isset($this->pool[$addr])) {return $this->pool[$addr];}
if (strpos($addr,'://') === FALSE) {$path = 'tcp://'.$addr;}
else {$path = $addr;}
if ($conn = fsockopen($path))
{
$this->pool[$addr] = $conn;
return $addr;
}
return FALSE;
}
private function getConnectionByKey($key)
{
if (($this->dtags_enabled) && (($sp = strpos($key,'[')) !== FALSE) && (($ep = strpos($key,']')) !== FALSE) && ($ep > $sp))
{
$key = substr($key,$sp+1,$ep-$sp-1);
}
srand(crc32($key));
$addr = array_rand($this->servers);
srand();
$this->getConnection($addr);
return $addr;
}
private function read($k,$len = NULL)
{
if ($len === 0) {return '';}
if ($len === NULL) {return fgets($this->pool[$k]);}
$r = '';
for (;;)
{
$l = strlen($r);
if ($l >= $len) {break;}
$r .= fread($this->pool[$k],min($len-$l,1024));
}
return $r;
}
private function write($k,$s)
{
return fwrite($this->pool[$k],$s);
}
public function requestByServer($k,$s)
{
if ($k == '*')
{
$result = array();
foreach ($this->servers as $k => $v)
{
$this->getConnection($k);
$this->write($k,$s."\r\n");
$result[$k] = $this->getResponse($k);
}
return $result;
}
if ($k === NULL)
{
srand();
$k = array_rand($this->servers);
}
$this->getConnection($k);
$this->write($k,$s."\r\n");
return $this->getResponse($k);
}
public function requestByKey($k,$s)
{
$k = $this->getConnectionByKey($k);
$this->write($k,$s."\r\n");
$r = $this->getResponse($k);
return $r;
}
private function disconnect($k)
{
if (!isset($this->pool[$k])) {return FALSE;}
fclose($this->pool[$k]);
unset($this->pool[$k]);
$this->pool = array_values($this->pool);
return TRUE;
}
private function getResponse($k)
{
if (($data = $this->read($k)) === FALSE) {return FALSE;}
$c = $data[0];
$data = substr($data,1);
if (substr($data,-2) == "\r\n") {$data = substr($data,0,-2);}
switch ($c)
{
case '-':
trigger_error($data, E_USER_WARNING);
return FALSE;
case '+':
return $data;
case ':':
return strpos($data, '.') !== FALSE ? (int)$data : (float)$data;
case '$':
return $this->getBulkReply($k,$c.$data);
case '*':
$num = (int)$data;
$result = array();
for ($i = 0; $i < $num; ++$i) {$result[] = $this->getResponse($k);}
return $result;
default:
trigger_error("Invalid reply type byte: '$c'");
return FALSE;
}
}
private function getBulkReply($k,$data)
{
if ($data === NULL) {$data = rtrim($this->read($k));}
if ($data == '$-1') {return NULL;}
if ($data[0] != '$') {trigger_error('Unknown response prefix for \''.$c.$data.'\'', E_USER_WARNING); return FALSE;}
$data = $this->read($k,(int) substr($data,1));
$end = $this->read($k,2);
if ($end != "\r\n") {trigger_error('Unknown response end: \''.$end.'\'', E_USER_WARNING); return FALSE;}
return $data;
}
public function ping($server = NULL)
{
return $this->requestByServer($server,'PING');
}
public function get($key,$plain = FALSE)
{
$r = $this->requestByKey($key,'GET '.$key);
if ($r === NULL) {return FALSE;}
return $plain?$r:json_decode($r,TRUE);
}
public function set($key,$value,$TTL = NULL)
{
if (!is_scalar($value)) {$value = json_encode($value);}
$r = $this->requestByKey($key,'SET '.$key.' '.strlen($value)."\r\n".$value);
if ($TTL !== NULL) {$this->expire($key,$TTL);}
if ($r === NULL) {return FALSE;}
return $r;
}
public function expire($key,$TTL = 0)
{
return $this->requestByKey($key,'EXPIRE '.$key.' '.$TTL);
}
public function getTTL($key)
{
return $this->requestByKey($key,'TTL '.$key);
}
public function add($key,$value,$TTL = 0)
{
if (!is_scalar($value)) {$value = json_encode($value);}
$r = $this->requestByKey($key,'SETNX '.$key.' '.strlen($value)."\r\n".$value);
if ($TTL > 0) {$this->expire($key,$TTL);}
return $r;
}
public function replace($key,$value,$TTL = 0) // not complete atomic
{
if (!$this->exists($key)) {return FALSE;}
$this->set($key,$value,$TTL);
return TRUE;
}
public function sendEcho($server = NULL,$s)
{
return $this->requestByServer($server,'ECHO '.strlen($s)."\r\n".$s);
}
public function getMultiByKey($keys,$bykey)
{
return $this->getMulti($keys,$this->getConnectionByKey($bykey));
}
public function getMulti($keys,$byserver = NULL)
{
if ($byserver !== NULL)
{
$result = array();
$values = $this->requestByServer($byserver,'MGET '.implode(' ',$keys));
$i = 0;
foreach ($keys as &$k) {$result[$k] = json_decode($values[$i++],TRUE);}
return $result;
}
elseif (sizeof($this->servers) <= 0) {return $this->getMulti($keys,end(array_keys($this->servers)));}
else
{
$result = array();
$batch = array();
foreach ($keys as $k)
{
$addr = $this->getConnectionByKey($k);
if (!isset($batch[$addr])) {$batch[$addr] = array();}
$batch[$addr][] = $k;
}
foreach ($batch as $s => $b) {$result = array_merge($result,$this->getMulti($b,$s));}
return $result;
}
}
public function increment($key,$number = 1)
{
if ($number == 1) {return $this->requestByKey($key,'INCR '.$key);}
return $this->requestByKey($key,'INCRBY '.$key.' '.$number);
}
public function decrement($key,$number = 1)
{
if ($number == 1) {return $this->requestByKey($key,'DECR '.$key);}
return $this->requestByKey($key,'DECRBY '.$key.' '.$number);
}
public function exists($key)
{
return $this->requestByKey($key,'EXISTS '.$key);
}
public function delete($key)
{
return $this->requestByKey($key,'DEL '.$key);
}
public function type($key)
{
return $this->requestByKey($key,'TYPE '.$key);
}
public function keys($pattern,$server = '')
{
if ($server === '') {$r = $this->requestByKey($pattern,'KEYS '.$pattern);}
else {$r = $this->requestByServer($server,'KEYS '.$pattern);}
return explode(' ',$r);
}
public function randomKey($server = NULL)
{
return $this->requestByServer($server,'RANDOMKEY');
}
public function rename($key,$newkey)
{
// need multi-server support
return $this->requestByKey($key,'RENAME '.$key.' '.$newkey);
}
public function renamenx($key,$newkey)
{
// need multi-server support
return $this->requestByKey($key,'RENAMENX '.$key.' '.$newkey);
}
public function push($key,$value,$right = TRUE)
{
return $this->requestByKey($key,($right?'RPUSH':'LPUSH').' '.$key.' '.strlen($value)."\r\n".$value);
}
public function lpush($key,$value)
{
return $this->push($key,$value,FALSE);
}
public function rpush($key,$value)
{
return $this->push($key,$value,TRUE);
}
public function cpush($maxsize,$key,$value,$right = TRUE)
{
return $this->requestByKey($key,($right?'CRPUSH':'CLPUSH').' '.$maxsize.' '.$key.' '.strlen($value)."\r\n".$value);
}
public function clpush($maxsize,$key,$value)
{
return $this->cpush($maxsize,$key,$value,FALSE);
}
public function crpush($maxsize,$key,$value)
{
return $this->cpush($maxsize,$key,$value,TRUE);
}
public function ltrim($key,$start,$end)
{
return $this->requestByKey($key,'LTRIM '.$key.' '.$start.' '.$end);
}
public function lindex($key,$index)
{
return $this->requestByKey($key,'LINDEX '.$key.' '.$index);
}
public function pop($key,$right = TRUE)
{
return $this->requestByKey($key,($right?'RPOP':'LPOP').' '.$key);
}
public function lpop($key)
{
return $this->pop($key,FALSE);
}
public function rpop($key)
{
return $this->pop($key,TRUE);
}
public function llen($key)
{
return $this->requestByKey($key,'LLEN '.$key);
}
public function lrange($key,$start,$end)
{
return $this->requestByKey($key,'LRANGE '.$key.' '.$start.' '.$end);
}
public function sort($key,$query = NULL)
{
return $this->requestByKey($key,'SORT '.$key.($query === NULL?'':' '.$query));
}
public function lset($key,$value,$index)
{
return $this->requestByKey($key,'LSET '.$key.' '.$index.' '.strlen($value)."\r\n".$value);
}
public function lrem($key,$count,$value)
{
return $this->requestByKey($key,'LREM '.$key.' '.$count.' '.strlen($value)."\r\n".$value);
}
public function sadd($key,$value)
{
return $this->requestByKey($key,'SADD '.$key.' '.strlen($value)."\r\n".$value);
}
public function srem($key,$value)
{
return $this->requestByKey($key,'SREM '.$key.' '.strlen($value)."\r\n".$value);
}
public function sismember($key,$value)
{
return $this->requestByKey($key,'SISMEMBER '.$key.' '.strlen($value)."\r\n".$value);
}
public function sinter($keys,$bykey)
{
return $this->requestByKey($bykey,'SINTER '.implode(' ',$keys));
}
public function sinterstore($keys,$bykey)
{
return $this->requestByKey($bykey,'SINTERSTORE '.$bykey.' '.implode(' ',$keys));
}
public function smembers($key)
{
return $this->requestByKey($key,'SMEMBERS '.$key);
}
public function scard($key)
{
return $this->requestByKey($key,'SCARD '.$key);
}
public function smove($srckey,$dstkey,$member)
{
return $this->requestByKey($key,'SMOVE '.$srckey.' '.$dstkey.' '.$member);
}
public function selectdb($dbname,$server = '*')
{
return $this->requestByServer($server,'SELECT '.$dbname);
}
public function move($key,$dbname)
{
return $this->requestByKey($key,'MOVE '.$key.' '.$dbname);
}
public function save($bg = FALSE,$server = '*')
{
return $this->requestByServer($server,($bg?'BGSAVE':'SAVE'));
}
public function lastsave($server = '*')
{
return $this->requestByServer($server,'LASTSAVE');
}
public function flush($all = FALSE,$server = '*')
{
return $this->requestByServer($server,($all?'FLUSHALL':'FLUSHDB'));
}
public function info($server = '*')
{
$r = $this->requestByServer($server,'INFO');
if ($server !== '*') {$r = array($server => $r);}
$result = array();
foreach ($r as $srv => $reply)
{
$info = array();
foreach (explode("\r\n",$reply) as $l)
{
if ($l === '') {continue;}
list($k,$v) = explode(':',$l,2);
$_v = strpos($v, '.') !== false ? (float)$v : (int)$v;
$info[$k] = (string)$_v == $v ? $_v : $v;
}
$result[$srv] = $info;
}
return $result;
}
}
Show details
Hide details
Change log
r133
by kak.serpom.po.yaitsam on Jul 2, 2009
Diff
[No log message]
Go to:
/trunk/IMemcacheClient.class.php
/trunk/Redis.class.php
Project members,
sign in
to write a code review
Older revisions
r132
by kak.serpom.po.yaitsam on Jul 2, 2009
Diff
[No log message]
r130
by kak.serpom.po.yaitsam on Jul 1, 2009
Diff
[No log message]
r129
by kak.serpom.po.yaitsam on Jul 1, 2009
Diff
[No log message]
All revisions of this file
File info
Size: 10688 bytes, 387 lines
View raw file
Powered by
Google Project Hosting