]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | <?php |
2 | /******************************************************************************* | |
3 | * Redis PHP Bindings - http://code.google.com/p/redis/ | |
4 | * | |
5 | * Copyright 2009 Ludovico Magnocavallo | |
6 | * Released under the same license as Redis. | |
7 | * | |
8 | * Version: 0.1 | |
9 | * | |
10 | * $Revision: 139 $ | |
11 | * $Date: 2009-03-15 22:59:40 +0100 (Dom, 15 Mar 2009) $ | |
12 | * | |
13 | ******************************************************************************/ | |
14 | ||
15 | ||
16 | class Redis { | |
17 | ||
18 | var $server; | |
19 | var $port; | |
20 | var $_sock; | |
21 | ||
bb7dcc1e | 22 | function Redis($host='localhost', $port=6379) { |
ed9b544e | 23 | $this->host = $host; |
24 | $this->port = $port; | |
25 | } | |
26 | ||
27 | function connect() { | |
28 | if ($this->_sock) | |
29 | return; | |
30 | if ($sock = fsockopen($this->host, $this->port, $errno, $errstr)) { | |
31 | $this->_sock = $sock; | |
32 | return; | |
33 | } | |
34 | $msg = "Cannot open socket to {$this->host}:{$this->port}"; | |
35 | if ($errno || $errmsg) | |
36 | $msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : ""); | |
37 | trigger_error("$msg.", E_USER_ERROR); | |
38 | } | |
39 | ||
40 | function disconnect() { | |
41 | if ($this->_sock) | |
42 | @fclose($this->_sock); | |
43 | $this->_sock = null; | |
44 | } | |
45 | ||
46 | function &ping() { | |
47 | $this->connect(); | |
48 | $this->_write("PING\r\n"); | |
bb7dcc1e | 49 | return $this->get_response(); |
ed9b544e | 50 | } |
51 | ||
52 | function &do_echo($s) { | |
53 | $this->connect(); | |
54 | $this->_write("ECHO " . strlen($s) . "\r\n$s\r\n"); | |
55 | return $this->_get_value(); | |
56 | } | |
57 | ||
58 | function &set($name, $value, $preserve=false) { | |
59 | $this->connect(); | |
60 | $this->_write( | |
61 | ($preserve ? 'SETNX' : 'SET') . | |
62 | " $name " . strlen($value) . "\r\n$value\r\n" | |
63 | ); | |
bb7dcc1e | 64 | return $this->get_response(); |
ed9b544e | 65 | } |
66 | ||
67 | function &get($name) { | |
68 | $this->connect(); | |
69 | $this->_write("GET $name\r\n"); | |
70 | return $this->_get_value(); | |
71 | } | |
72 | ||
73 | function &incr($name, $amount=1) { | |
74 | $this->connect(); | |
75 | if ($amount == 1) | |
76 | $this->_write("INCR $name\r\n"); | |
77 | else | |
78 | $this->_write("INCRBY $name $amount\r\n"); | |
bb7dcc1e | 79 | return $this->get_response(); |
ed9b544e | 80 | } |
81 | ||
82 | function &decr($name, $amount=1) { | |
83 | $this->connect(); | |
84 | if ($amount == 1) | |
85 | $this->_write("DECR $name\r\n"); | |
86 | else | |
87 | $this->_write("DECRBY $name $amount\r\n"); | |
bb7dcc1e | 88 | return $this->get_response(); |
ed9b544e | 89 | } |
90 | ||
91 | function &exists($name) { | |
92 | $this->connect(); | |
93 | $this->_write("EXISTS $name\r\n"); | |
bb7dcc1e | 94 | return $this->get_response(); |
ed9b544e | 95 | } |
96 | ||
97 | function &delete($name) { | |
98 | $this->connect(); | |
99 | $this->_write("DEL $name\r\n"); | |
bb7dcc1e | 100 | return $this->get_response(); |
ed9b544e | 101 | } |
102 | ||
103 | function &keys($pattern) { | |
104 | $this->connect(); | |
105 | $this->_write("KEYS $pattern\r\n"); | |
106 | return explode(' ', $this->_get_value()); | |
107 | } | |
108 | ||
109 | function &randomkey() { | |
110 | $this->connect(); | |
111 | $this->_write("RANDOMKEY\r\n"); | |
bb7dcc1e | 112 | return $this->get_response(); |
ed9b544e | 113 | } |
114 | ||
115 | function &rename($src, $dst, $preserve=False) { | |
116 | $this->connect(); | |
bb7dcc1e LM |
117 | $this->_write($preserve ? "RENAMENX $src $dst\r\n" : "RENAME $src $dst\r\n"); |
118 | return $this->get_response(); | |
ed9b544e | 119 | } |
120 | ||
121 | function &push($name, $value, $tail=true) { | |
122 | // default is to append the element to the list | |
123 | $this->connect(); | |
124 | $this->_write( | |
125 | ($tail ? 'RPUSH' : 'LPUSH') . | |
126 | " $name " . strlen($value) . "\r\n$value\r\n" | |
127 | ); | |
bb7dcc1e | 128 | return $this->get_response(); |
ed9b544e | 129 | } |
130 | ||
131 | function <rim($name, $start, $end) { | |
132 | $this->connect(); | |
133 | $this->_write("LTRIM $name $start $end\r\n"); | |
bb7dcc1e | 134 | return $this->get_response(); |
ed9b544e | 135 | } |
136 | ||
137 | function &lindex($name, $index) { | |
138 | $this->connect(); | |
139 | $this->_write("LINDEX $name $index\r\n"); | |
140 | return $this->_get_value(); | |
141 | } | |
142 | ||
143 | function &pop($name, $tail=true) { | |
144 | $this->connect(); | |
145 | $this->_write( | |
146 | ($tail ? 'RPOP' : 'LPOP') . | |
147 | " $name\r\n" | |
148 | ); | |
149 | return $this->_get_value(); | |
150 | } | |
151 | ||
152 | function &llen($name) { | |
153 | $this->connect(); | |
154 | $this->_write("LLEN $name\r\n"); | |
bb7dcc1e | 155 | return $this->get_response(); |
ed9b544e | 156 | } |
157 | ||
158 | function &lrange($name, $start, $end) { | |
159 | $this->connect(); | |
160 | $this->_write("LRANGE $name $start $end\r\n"); | |
bb7dcc1e | 161 | return $this->get_response(); |
ed9b544e | 162 | } |
163 | ||
164 | function &sort($name, $query=false) { | |
165 | $this->connect(); | |
bb7dcc1e LM |
166 | $this->_write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n"); |
167 | return $this->get_response(); | |
ed9b544e | 168 | } |
169 | ||
170 | function &lset($name, $value, $index) { | |
171 | $this->connect(); | |
172 | $this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n"); | |
bb7dcc1e | 173 | return $this->get_response(); |
ed9b544e | 174 | } |
175 | ||
176 | function &sadd($name, $value) { | |
177 | $this->connect(); | |
178 | $this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n"); | |
bb7dcc1e | 179 | return $this->get_response(); |
ed9b544e | 180 | } |
181 | ||
182 | function &srem($name, $value) { | |
183 | $this->connect(); | |
184 | $this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n"); | |
bb7dcc1e | 185 | return $this->get_response(); |
ed9b544e | 186 | } |
187 | ||
188 | function &sismember($name, $value) { | |
189 | $this->connect(); | |
190 | $this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n"); | |
bb7dcc1e | 191 | return $this->get_response(); |
ed9b544e | 192 | } |
193 | ||
194 | function &sinter($sets) { | |
195 | $this->connect(); | |
196 | $this->_write('SINTER ' . implode(' ', $sets) . "\r\n"); | |
bb7dcc1e | 197 | return $this->get_response(); |
ed9b544e | 198 | } |
199 | ||
200 | function &smembers($name) { | |
201 | $this->connect(); | |
202 | $this->_write("SMEMBERS $name\r\n"); | |
bb7dcc1e | 203 | return $this->get_response(); |
ed9b544e | 204 | } |
205 | ||
206 | function &scard($name) { | |
207 | $this->connect(); | |
208 | $this->_write("SCARD $name\r\n"); | |
bb7dcc1e | 209 | return $this->get_response(); |
ed9b544e | 210 | } |
211 | ||
212 | function &select_db($name) { | |
213 | $this->connect(); | |
214 | $this->_write("SELECT $name\r\n"); | |
bb7dcc1e | 215 | return $this->get_response(); |
ed9b544e | 216 | } |
217 | ||
218 | function &move($name, $db) { | |
219 | $this->connect(); | |
220 | $this->_write("MOVE $name $db\r\n"); | |
bb7dcc1e | 221 | return $this->get_response(); |
ed9b544e | 222 | } |
223 | ||
224 | function &save($background=false) { | |
225 | $this->connect(); | |
226 | $this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n")); | |
bb7dcc1e | 227 | return $this->get_response(); |
ed9b544e | 228 | } |
229 | ||
230 | function &lastsave() { | |
231 | $this->connect(); | |
232 | $this->_write("LASTSAVE\r\n"); | |
bb7dcc1e LM |
233 | return $this->get_response(); |
234 | } | |
235 | ||
236 | function &flush($all=false) { | |
237 | $this->connect(); | |
238 | $this->_write($all ? "FLUSH\r\n" : "FLUSHDB\r\n"); | |
239 | return $this->get_response(); | |
240 | } | |
241 | ||
242 | function &info() { | |
243 | $this->connect(); | |
244 | $this->_write("INFO\r\n"); | |
245 | $info = array(); | |
246 | $data =& $this->get_response(); | |
247 | foreach (explode("\r\n", $data) as $l) { | |
248 | if (!$l) | |
249 | continue; | |
250 | list($k, $v) = explode(':', $l, 2); | |
251 | $_v = strpos($v, '.') !== false ? (float)$v : (int)$v; | |
252 | $info[$k] = (string)$_v == $v ? $_v : $v; | |
253 | } | |
254 | return $info; | |
ed9b544e | 255 | } |
256 | ||
257 | function &_write($s) { | |
258 | while ($s) { | |
259 | $i = fwrite($this->_sock, $s); | |
bb7dcc1e | 260 | if ($i == 0) // || $i == strlen($s)) |
ed9b544e | 261 | break; |
262 | $s = substr($s, $i); | |
263 | } | |
264 | } | |
265 | ||
266 | function &_read($len=1024) { | |
267 | if ($s = fgets($this->_sock)) | |
268 | return $s; | |
269 | $this->disconnect(); | |
270 | trigger_error("Cannot read from socket.", E_USER_ERROR); | |
271 | } | |
272 | ||
bb7dcc1e LM |
273 | function &get_response() { |
274 | $data = trim($this->_read()); | |
275 | $c = $data[0]; | |
276 | $data = substr($data, 1); | |
277 | switch ($c) { | |
278 | case '-': | |
279 | trigger_error(substr($data, 0, 4) == 'ERR ' ? substr($data, 4) : $data, E_USER_ERROR); | |
280 | break; | |
281 | case '+': | |
282 | return $data; | |
283 | case '*': | |
284 | $num = (int)$data; | |
285 | if ((string)$num != $data) | |
286 | trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR); | |
287 | $result = array(); | |
288 | for ($i=0; $i<$num; $i++) | |
289 | $result[] =& $this->_get_value(); | |
290 | return $result; | |
291 | default: | |
292 | return $this->_get_value($c . $data); | |
ed9b544e | 293 | } |
bb7dcc1e LM |
294 | } |
295 | ||
296 | function &_get_value($data=null) { | |
297 | if ($data === null) | |
298 | $data =& trim($this->_read()); | |
299 | if ($data == '$-1') | |
ed9b544e | 300 | return null; |
bb7dcc1e LM |
301 | $c = $data[0]; |
302 | $data = substr($data, 1); | |
303 | $i = strpos($data, '.') !== false ? (int)$data : (float)$data; | |
304 | if ((string)$i != $data) | |
305 | trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR); | |
306 | if ($c == ':') | |
307 | return $i; | |
308 | if ($c != '$') | |
309 | trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR); | |
ed9b544e | 310 | $buffer = ''; |
bb7dcc1e LM |
311 | while (true) { |
312 | $data =& $this->_read(); | |
313 | $i -= strlen($data); | |
314 | $buffer .= $data; | |
315 | if ($i < 0) | |
316 | break; | |
ed9b544e | 317 | } |
bb7dcc1e | 318 | return substr($buffer, 0, -2); |
ed9b544e | 319 | } |
320 | ||
321 | } | |
322 | ||
bb7dcc1e LM |
323 | //$r =& new Redis(); |
324 | //var_dump($r->info()); | |
ed9b544e | 325 | |
326 | ?> |