]>
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 * Copyright 2009 Salvatore Sanfilippo (ported it to PHP5, fixed some bug)
7 * Released under the same license as Redis.
12 * $Date: 2009-03-15 22:59:40 +0100 (Dom, 15 Mar 2009) $
14 ******************************************************************************/
22 public function __construct($host='localhost', $port=6379) {
27 public function connect() {
28 if ($this->_sock
) return;
29 if ($sock = fsockopen($this->host
, $this->port
, $errno, $errstr)) {
33 $msg = "Cannot open socket to {$this->host}:{$this->port}";
34 if ($errno || $errmsg)
35 $msg .= "," . ($errno ? " error $errno" : "") .
36 ($errmsg ? " $errmsg" : "");
37 trigger_error("$msg.", E_USER_ERROR
);
40 public function disconnect() {
41 if ($this->_sock
) @fclose($this->_sock
);
45 public function ping() {
47 $this->write("PING\r\n");
48 return $this->get_response();
51 public function do_echo($s) {
53 $this->write("ECHO " . strlen($s) . "\r\n$s\r\n");
54 return $this->get_response();
57 public function set($name, $value, $preserve=false) {
60 ($preserve ? 'SETNX' : 'SET') .
61 " $name " . strlen($value) . "\r\n$value\r\n"
63 return $this->get_response();
66 public function get($name) {
68 $this->write("GET $name\r\n");
69 return $this->get_response();
72 public function incr($name, $amount=1) {
75 $this->write("INCR $name\r\n");
77 $this->write("INCRBY $name $amount\r\n");
78 return $this->get_response();
81 public function decr($name, $amount=1) {
84 $this->write("DECR $name\r\n");
86 $this->write("DECRBY $name $amount\r\n");
87 return $this->get_response();
90 public function exists($name) {
92 $this->write("EXISTS $name\r\n");
93 return $this->get_response();
96 public function delete($name) {
98 $this->write("DEL $name\r\n");
99 return $this->get_response();
102 public function keys($pattern) {
104 $this->write("KEYS $pattern\r\n");
105 return explode(' ', $this->get_response());
108 public function randomkey() {
110 $this->write("RANDOMKEY\r\n");
111 return $this->get_response();
114 public function rename($src, $dst) {
116 $this->write("RENAME $src $dst\r\n");
117 return $this->get_response();
120 public function renamenx($src, $dst) {
122 $this->write("RENAMENX $src $dst\r\n");
123 return $this->get_response();
126 public function expire($name, $time) {
128 $this->write("EXPIRE $name $time\r\n");
129 return $this->get_response();
132 public function push($name, $value, $tail=true) {
133 // default is to append the element to the list
136 ($tail ? 'RPUSH' : 'LPUSH') .
137 " $name " . strlen($value) . "\r\n$value\r\n"
139 return $this->get_response();
142 public function lpush($name, $value) {
143 return $this->push($name, $value, false);
146 public function rpush($name, $value) {
147 return $this->push($name, $value, true);
150 public function ltrim($name, $start, $end) {
152 $this->write("LTRIM $name $start $end\r\n");
153 return $this->get_response();
156 public function lindex($name, $index) {
158 $this->write("LINDEX $name $index\r\n");
159 return $this->get_response();
162 public function pop($name, $tail=true) {
165 ($tail ? 'RPOP' : 'LPOP') .
168 return $this->get_response();
171 public function lpop($name, $value) {
172 return $this->pop($name, $value, false);
175 public function rpop($name, $value) {
176 return $this->pop($name, $value, true);
179 public function llen($name) {
181 $this->write("LLEN $name\r\n");
182 return $this->get_response();
185 public function lrange($name, $start, $end) {
187 $this->write("LRANGE $name $start $end\r\n");
188 return $this->get_response();
191 public function sort($name, $query=false) {
193 $this->write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n");
194 return $this->get_response();
197 public function lset($name, $value, $index) {
199 $this->write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
200 return $this->get_response();
203 public function sadd($name, $value) {
205 $this->write("SADD $name " . strlen($value) . "\r\n$value\r\n");
206 return $this->get_response();
209 public function srem($name, $value) {
211 $this->write("SREM $name " . strlen($value) . "\r\n$value\r\n");
212 return $this->get_response();
215 public function sismember($name, $value) {
217 $this->write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
218 return $this->get_response();
221 public function sinter($sets) {
223 $this->write('SINTER ' . implode(' ', $sets) . "\r\n");
224 return $this->get_response();
227 public function smembers($name) {
229 $this->write("SMEMBERS $name\r\n");
230 return $this->get_response();
233 public function scard($name) {
235 $this->write("SCARD $name\r\n");
236 return $this->get_response();
239 public function select_db($name) {
241 $this->write("SELECT $name\r\n");
242 return $this->get_response();
245 public function move($name, $db) {
247 $this->write("MOVE $name $db\r\n");
248 return $this->get_response();
251 public function save($background=false) {
253 $this->write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
254 return $this->get_response();
257 public function bgsave($background=false) {
258 return $this->save(true);
261 public function lastsave() {
263 $this->write("LASTSAVE\r\n");
264 return $this->get_response();
267 public function flushdb($all=false) {
269 $this->write($all ? "FLUSHALL\r\n" : "FLUSHDB\r\n");
270 return $this->get_response();
273 public function flushall() {
274 return $this->flush(true);
277 public function info() {
279 $this->write("INFO\r\n");
281 $data =& $this->get_response();
282 foreach (explode("\r\n", $data) as $l) {
285 list($k, $v) = explode(':', $l, 2);
286 $_v = strpos($v, '.') !== false ? (float)$v : (int)$v;
287 $info[$k] = (string)$_v == $v ? $_v : $v;
292 private function write($s) {
294 $i = fwrite($this->_sock
, $s);
295 if ($i == 0) // || $i == strlen($s))
301 private function read($len=1024) {
302 if ($s = fgets($this->_sock
))
305 trigger_error("Cannot read from socket.", E_USER_ERROR
);
308 private function get_response() {
309 $data = trim($this->read());
311 $data = substr($data, 1);
314 trigger_error($data, E_USER_ERROR
);
319 $i = strpos($data, '.') !== false ? (int)$data : (float)$data;
320 if ((string)$i != $data)
321 trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR
);
324 return $this->get_bulk_reply($c . $data);
327 if ((string)$num != $data)
328 trigger_error("Cannot convert multi-response header '$data' to
integer", E_USER_ERROR);
330 for ($i=0; $i<$num; $i++)
331 $result[] =& $this->get_response();
334 trigger_error("Invalid reply type byte
: '$c'");
338 private function get_bulk_reply($data=null) {
340 $data = trim($this->read());
344 $data = substr($data, 1);
345 $bulklen = (int)$data;
346 if ((string)$bulklen != $data)
347 trigger_error("Cannot convert bulk read header '$c$data' to integer", E_USER_ERROR
);
349 trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR
);
352 $data = fread($this->_sock
,$bulklen);
353 $bulklen -= strlen($data);
356 $crlf = fread($this->_sock
,2);
362 var_dump($r->get("foo"));
363 var_dump($r->info());