]>
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, $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->_simple_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 $preserve ? $this->_numeric_response() : $this->_simple_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->_numeric_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->_numeric_response();
91 function &exists($name) {
93 $this->_write("EXISTS $name\r\n");
94 return $this->_numeric_response();
97 function &delete($name) {
99 $this->_write("DEL $name\r\n");
100 return $this->_numeric_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 $s =& trim($this->_read());
113 $this->_check_for_error($s);
117 function &rename($src, $dst, $preserve=False) {
120 $this->_write("RENAMENX $src $dst\r\n");
121 return $this->_numeric_response();
123 $this->_write("RENAME $src $dst\r\n");
124 return trim($this->_simple_response());
127 function &push($name, $value, $tail=true) {
128 // default is to append the element to the list
131 ($tail ? 'RPUSH' : 'LPUSH') .
132 " $name " . strlen($value) . "\r\n$value\r\n"
134 return $this->_simple_response();
137 function <rim($name, $start, $end) {
139 $this->_write("LTRIM $name $start $end\r\n");
140 return $this->_simple_response();
143 function &lindex($name, $index) {
145 $this->_write("LINDEX $name $index\r\n");
146 return $this->_get_value();
149 function &pop($name, $tail=true) {
152 ($tail ? 'RPOP' : 'LPOP') .
155 return $this->_get_value();
158 function &llen($name) {
160 $this->_write("LLEN $name\r\n");
161 return $this->_numeric_response();
164 function &lrange($name, $start, $end) {
166 $this->_write("LRANGE $name $start $end\r\n");
167 return $this->_get_multi();
170 function &sort($name, $query=false) {
172 if ($query === false) {
173 $this->_write("SORT $name\r\n");
175 $this->_write("SORT $name $query\r\n");
177 return $this->_get_multi();
180 function &lset($name, $value, $index) {
182 $this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
183 return $this->_simple_response();
186 function &sadd($name, $value) {
188 $this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n");
189 return $this->_numeric_response();
192 function &srem($name, $value) {
194 $this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n");
195 return $this->_numeric_response();
198 function &sismember($name, $value) {
200 $this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
201 return $this->_numeric_response();
204 function &sinter($sets) {
206 $this->_write('SINTER ' . implode(' ', $sets) . "\r\n");
207 return $this->_get_multi();
210 function &smembers($name) {
212 $this->_write("SMEMBERS $name\r\n");
213 return $this->_get_multi();
216 function &scard($name) {
218 $this->_write("SCARD $name\r\n");
219 return $this->_numeric_response();
222 function &select_db($name) {
224 $this->_write("SELECT $name\r\n");
225 return $this->_simple_response();
228 function &move($name, $db) {
230 $this->_write("MOVE $name $db\r\n");
231 return $this->_numeric_response();
234 function &save($background=false) {
236 $this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
237 return $this->_simple_response();
240 function &lastsave() {
242 $this->_write("LASTSAVE\r\n");
243 return $this->_numeric_response();
246 function &_write($s) {
248 $i = fwrite($this->_sock
, $s);
255 function &_read($len=1024) {
256 if ($s = fgets($this->_sock
))
259 trigger_error("Cannot read from socket.", E_USER_ERROR
);
262 function _check_for_error(&$s) {
263 if (!$s || $s[0] != '-')
265 if (substr($s, 0, 4) == '-ERR')
266 trigger_error("Redis error: " . trim(substr($s, 4)), E_USER_ERROR
);
267 trigger_error("Redis error: " . substr(trim($this->_read()), 5), E_USER_ERROR
);
270 function &_simple_response() {
271 $s =& trim($this->_read());
273 return substr($s, 1);
274 if ($err =& $this->_check_for_error($s))
276 trigger_error("Cannot parse first line '$s' for a simple response", E_USER_ERROR
);
279 function &_numeric_response($allow_negative=True) {
280 $s =& trim($this->_read());
283 if (!$allow_negative && $i < 0)
284 $this->_check_for_error($s);
289 trigger_error("Cannot parse '$s' as numeric response.");
292 function &_get_value() {
293 $s =& trim($this->_read());
296 else if ($s[0] == '-')
297 $this->_check_for_error($s);
300 trigger_error("Cannot parse '$s' as data length.");
306 if ($l > $i) // ending crlf
310 if ($i == 0) // let's restore the trailing crlf
311 $buffer .= $this->_read();
315 function &_get_multi() {
317 $num =& $this->_numeric_response(false);
321 $results[] =& $this->_get_value();