]>
git.saurik.com Git - redis.git/blob - client-libraries/php/redis.php
2 /*******************************************************************************
3 * Redis PHP Bindings - http://code.google.com/p/redis/
5 * Copyright 2009 Ludovico Magnocavallo
6 * Released under the same license as Redis.
11 * $Date: 2009-03-15 22:59:40 +0100 (Dom, 15 Mar 2009) $
13 ******************************************************************************/
22 function Redis($host='localhost', $port=6379) {
30 if ($sock = fsockopen($this->host
, $this->port
, $errno, $errstr)) {
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
);
40 function disconnect() {
42 @fclose($this->_sock
);
48 $this->_write("PING\r\n");
49 return $this->get_response();
52 function &do_echo($s) {
54 $this->_write("ECHO " . strlen($s) . "\r\n$s\r\n");
55 return $this->_get_value();
58 function &set($name, $value, $preserve=false) {
61 ($preserve ? 'SETNX' : 'SET') .
62 " $name " . strlen($value) . "\r\n$value\r\n"
64 return $this->get_response();
67 function &get($name) {
69 $this->_write("GET $name\r\n");
70 return $this->_get_value();
73 function &incr($name, $amount=1) {
76 $this->_write("INCR $name\r\n");
78 $this->_write("INCRBY $name $amount\r\n");
79 return $this->get_response();
82 function &decr($name, $amount=1) {
85 $this->_write("DECR $name\r\n");
87 $this->_write("DECRBY $name $amount\r\n");
88 return $this->get_response();
91 function &exists($name) {
93 $this->_write("EXISTS $name\r\n");
94 return $this->get_response();
97 function &delete($name) {
99 $this->_write("DEL $name\r\n");
100 return $this->get_response();
103 function &keys($pattern) {
105 $this->_write("KEYS $pattern\r\n");
106 return explode(' ', $this->_get_value());
109 function &randomkey() {
111 $this->_write("RANDOMKEY\r\n");
112 return $this->get_response();
115 function &rename($src, $dst, $preserve=False) {
117 $this->_write($preserve ? "RENAMENX $src $dst\r\n" : "RENAME $src $dst\r\n");
118 return $this->get_response();
121 function &push($name, $value, $tail=true) {
122 // default is to append the element to the list
125 ($tail ? 'RPUSH' : 'LPUSH') .
126 " $name " . strlen($value) . "\r\n$value\r\n"
128 return $this->get_response();
131 function <rim($name, $start, $end) {
133 $this->_write("LTRIM $name $start $end\r\n");
134 return $this->get_response();
137 function &lindex($name, $index) {
139 $this->_write("LINDEX $name $index\r\n");
140 return $this->_get_value();
143 function &pop($name, $tail=true) {
146 ($tail ? 'RPOP' : 'LPOP') .
149 return $this->_get_value();
152 function &llen($name) {
154 $this->_write("LLEN $name\r\n");
155 return $this->get_response();
158 function &lrange($name, $start, $end) {
160 $this->_write("LRANGE $name $start $end\r\n");
161 return $this->get_response();
164 function &sort($name, $query=false) {
166 $this->_write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n");
167 return $this->get_response();
170 function &lset($name, $value, $index) {
172 $this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
173 return $this->get_response();
176 function &sadd($name, $value) {
178 $this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n");
179 return $this->get_response();
182 function &srem($name, $value) {
184 $this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n");
185 return $this->get_response();
188 function &sismember($name, $value) {
190 $this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
191 return $this->get_response();
194 function &sinter($sets) {
196 $this->_write('SINTER ' . implode(' ', $sets) . "\r\n");
197 return $this->get_response();
200 function &smembers($name) {
202 $this->_write("SMEMBERS $name\r\n");
203 return $this->get_response();
206 function &scard($name) {
208 $this->_write("SCARD $name\r\n");
209 return $this->get_response();
212 function &select_db($name) {
214 $this->_write("SELECT $name\r\n");
215 return $this->get_response();
218 function &move($name, $db) {
220 $this->_write("MOVE $name $db\r\n");
221 return $this->get_response();
224 function &save($background=false) {
226 $this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
227 return $this->get_response();
230 function &lastsave() {
232 $this->_write("LASTSAVE\r\n");
233 return $this->get_response();
236 function &flush($all=false) {
238 $this->_write($all ? "FLUSH\r\n" : "FLUSHDB\r\n");
239 return $this->get_response();
244 $this->_write("INFO\r\n");
246 $data =& $this->get_response();
247 foreach (explode("\r\n", $data) as $l) {
250 list($k, $v) = explode(':', $l, 2);
251 $_v = strpos($v, '.') !== false ? (float)$v : (int)$v;
252 $info[$k] = (string)$_v == $v ? $_v : $v;
257 function &_write($s) {
259 $i = fwrite($this->_sock
, $s);
260 if ($i == 0) // || $i == strlen($s))
266 function &_read($len=1024) {
267 if ($s = fgets($this->_sock
))
270 trigger_error("Cannot read from socket.", E_USER_ERROR
);
273 function &get_response() {
274 $data = trim($this->_read());
276 $data = substr($data, 1);
279 trigger_error(substr($data, 0, 4) == 'ERR ' ? substr($data, 4) : $data, E_USER_ERROR
);
285 if ((string)$num != $data)
286 trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR
);
288 for ($i=0; $i<$num; $i++
)
289 $result[] =& $this->_get_value();
292 return $this->_get_value($c . $data);
296 function &_get_value($data=null) {
298 $data =& trim($this->_read());
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);
309 trigger_error("Unkown response prefix
for '$c$data'", E_USER_ERROR);
312 $data =& $this->_read();
318 return substr($buffer, 0, -2);
324 //var_dump($r->info());