]> git.saurik.com Git - redis.git/blame - client-libraries/php/redis.php
doc update
[redis.git] / client-libraries / php / redis.php
CommitLineData
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
16class 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
9a2944ac
LM
121 function &expire($name, $time) {
122 $this->connect();
123 $this->_write("EXPIRE $name $time\r\n");
124 return $this->get_response();
125 }
126
ed9b544e 127 function &push($name, $value, $tail=true) {
128 // default is to append the element to the list
129 $this->connect();
130 $this->_write(
131 ($tail ? 'RPUSH' : 'LPUSH') .
132 " $name " . strlen($value) . "\r\n$value\r\n"
133 );
bb7dcc1e 134 return $this->get_response();
ed9b544e 135 }
136
137 function &ltrim($name, $start, $end) {
138 $this->connect();
139 $this->_write("LTRIM $name $start $end\r\n");
bb7dcc1e 140 return $this->get_response();
ed9b544e 141 }
142
143 function &lindex($name, $index) {
144 $this->connect();
145 $this->_write("LINDEX $name $index\r\n");
146 return $this->_get_value();
147 }
148
149 function &pop($name, $tail=true) {
150 $this->connect();
151 $this->_write(
152 ($tail ? 'RPOP' : 'LPOP') .
153 " $name\r\n"
154 );
155 return $this->_get_value();
156 }
157
158 function &llen($name) {
159 $this->connect();
160 $this->_write("LLEN $name\r\n");
bb7dcc1e 161 return $this->get_response();
ed9b544e 162 }
163
164 function &lrange($name, $start, $end) {
165 $this->connect();
166 $this->_write("LRANGE $name $start $end\r\n");
bb7dcc1e 167 return $this->get_response();
ed9b544e 168 }
169
170 function &sort($name, $query=false) {
171 $this->connect();
bb7dcc1e
LM
172 $this->_write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n");
173 return $this->get_response();
ed9b544e 174 }
175
176 function &lset($name, $value, $index) {
177 $this->connect();
178 $this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
bb7dcc1e 179 return $this->get_response();
ed9b544e 180 }
181
182 function &sadd($name, $value) {
183 $this->connect();
184 $this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n");
bb7dcc1e 185 return $this->get_response();
ed9b544e 186 }
187
188 function &srem($name, $value) {
189 $this->connect();
190 $this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n");
bb7dcc1e 191 return $this->get_response();
ed9b544e 192 }
193
194 function &sismember($name, $value) {
195 $this->connect();
196 $this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
bb7dcc1e 197 return $this->get_response();
ed9b544e 198 }
199
200 function &sinter($sets) {
201 $this->connect();
202 $this->_write('SINTER ' . implode(' ', $sets) . "\r\n");
bb7dcc1e 203 return $this->get_response();
ed9b544e 204 }
205
206 function &smembers($name) {
207 $this->connect();
208 $this->_write("SMEMBERS $name\r\n");
bb7dcc1e 209 return $this->get_response();
ed9b544e 210 }
211
212 function &scard($name) {
213 $this->connect();
214 $this->_write("SCARD $name\r\n");
bb7dcc1e 215 return $this->get_response();
ed9b544e 216 }
217
218 function &select_db($name) {
219 $this->connect();
220 $this->_write("SELECT $name\r\n");
bb7dcc1e 221 return $this->get_response();
ed9b544e 222 }
223
224 function &move($name, $db) {
225 $this->connect();
226 $this->_write("MOVE $name $db\r\n");
bb7dcc1e 227 return $this->get_response();
ed9b544e 228 }
229
230 function &save($background=false) {
231 $this->connect();
232 $this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
bb7dcc1e 233 return $this->get_response();
ed9b544e 234 }
235
236 function &lastsave() {
237 $this->connect();
238 $this->_write("LASTSAVE\r\n");
bb7dcc1e
LM
239 return $this->get_response();
240 }
241
242 function &flush($all=false) {
243 $this->connect();
244 $this->_write($all ? "FLUSH\r\n" : "FLUSHDB\r\n");
245 return $this->get_response();
246 }
247
248 function &info() {
249 $this->connect();
250 $this->_write("INFO\r\n");
251 $info = array();
252 $data =& $this->get_response();
253 foreach (explode("\r\n", $data) as $l) {
254 if (!$l)
255 continue;
256 list($k, $v) = explode(':', $l, 2);
257 $_v = strpos($v, '.') !== false ? (float)$v : (int)$v;
258 $info[$k] = (string)$_v == $v ? $_v : $v;
259 }
260 return $info;
ed9b544e 261 }
262
263 function &_write($s) {
264 while ($s) {
265 $i = fwrite($this->_sock, $s);
bb7dcc1e 266 if ($i == 0) // || $i == strlen($s))
ed9b544e 267 break;
268 $s = substr($s, $i);
269 }
270 }
271
272 function &_read($len=1024) {
273 if ($s = fgets($this->_sock))
274 return $s;
275 $this->disconnect();
276 trigger_error("Cannot read from socket.", E_USER_ERROR);
277 }
278
bb7dcc1e
LM
279 function &get_response() {
280 $data = trim($this->_read());
281 $c = $data[0];
282 $data = substr($data, 1);
283 switch ($c) {
284 case '-':
285 trigger_error(substr($data, 0, 4) == 'ERR ' ? substr($data, 4) : $data, E_USER_ERROR);
286 break;
287 case '+':
288 return $data;
289 case '*':
290 $num = (int)$data;
291 if ((string)$num != $data)
292 trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR);
293 $result = array();
294 for ($i=0; $i<$num; $i++)
295 $result[] =& $this->_get_value();
296 return $result;
297 default:
298 return $this->_get_value($c . $data);
ed9b544e 299 }
bb7dcc1e
LM
300 }
301
302 function &_get_value($data=null) {
303 if ($data === null)
304 $data =& trim($this->_read());
305 if ($data == '$-1')
ed9b544e 306 return null;
bb7dcc1e
LM
307 $c = $data[0];
308 $data = substr($data, 1);
309 $i = strpos($data, '.') !== false ? (int)$data : (float)$data;
310 if ((string)$i != $data)
311 trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR);
312 if ($c == ':')
313 return $i;
314 if ($c != '$')
315 trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR);
ed9b544e 316 $buffer = '';
bb7dcc1e
LM
317 while (true) {
318 $data =& $this->_read();
319 $i -= strlen($data);
320 $buffer .= $data;
321 if ($i < 0)
322 break;
ed9b544e 323 }
bb7dcc1e 324 return substr($buffer, 0, -2);
ed9b544e 325 }
326
327}
328
bb7dcc1e
LM
329//$r =& new Redis();
330//var_dump($r->info());
ed9b544e 331
332?>