]>
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 &expire($name, $time) { 
 123         $this->_write("EXPIRE $name $time\r\n"); 
 124         return $this->get_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->get_response(); 
 137     function <rim($name, $start, $end) { 
 139         $this->_write("LTRIM $name $start $end\r\n"); 
 140         return $this->get_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->get_response(); 
 164     function &lrange($name, $start, $end) { 
 166         $this->_write("LRANGE $name $start $end\r\n"); 
 167         return $this->get_response(); 
 170     function &sort($name, $query=false) { 
 172         $this->_write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n"); 
 173         return $this->get_response(); 
 176     function &lset($name, $value, $index) { 
 178         $this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n"); 
 179         return $this->get_response(); 
 182     function &sadd($name, $value) { 
 184         $this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n"); 
 185         return $this->get_response(); 
 188     function &srem($name, $value) { 
 190         $this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n"); 
 191         return $this->get_response(); 
 194     function &sismember($name, $value) { 
 196         $this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n"); 
 197         return $this->get_response(); 
 200     function &sinter($sets) { 
 202         $this->_write('SINTER ' . implode(' ', $sets) . "\r\n"); 
 203         return $this->get_response(); 
 206     function &smembers($name) { 
 208         $this->_write("SMEMBERS $name\r\n"); 
 209         return $this->get_response(); 
 212     function &scard($name) { 
 214         $this->_write("SCARD $name\r\n"); 
 215         return $this->get_response(); 
 218     function &select_db($name) { 
 220         $this->_write("SELECT $name\r\n"); 
 221         return $this->get_response(); 
 224     function &move($name, $db) { 
 226         $this->_write("MOVE $name $db\r\n"); 
 227         return $this->get_response(); 
 230     function &save($background=false) { 
 232         $this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n")); 
 233         return $this->get_response(); 
 236     function &lastsave() { 
 238         $this->_write("LASTSAVE\r\n"); 
 239         return $this->get_response(); 
 242     function &flush($all=false) { 
 244         $this->_write($all ? "FLUSH\r\n" : "FLUSHDB\r\n"); 
 245         return $this->get_response(); 
 250         $this->_write("INFO\r\n"); 
 252         $data =& $this->get_response(); 
 253         foreach (explode("\r\n", $data) as $l) { 
 256             list($k, $v) = explode(':', $l, 2); 
 257             $_v = strpos($v, '.') !== false ? (float)$v : (int)$v; 
 258             $info[$k] = (string)$_v == $v ? $_v : $v; 
 263     function &_write($s) { 
 265             $i = fwrite($this->_sock
, $s); 
 266             if ($i == 0) // || $i == strlen($s)) 
 272     function &_read($len=1024) { 
 273         if ($s = fgets($this->_sock
)) 
 276         trigger_error("Cannot read from socket.", E_USER_ERROR
); 
 279     function &get_response() { 
 280         $data = trim($this->_read()); 
 282         $data = substr($data, 1); 
 285                 trigger_error(substr($data, 0, 4) == 'ERR ' ? substr($data, 4) : $data, E_USER_ERROR
); 
 291                 if ((string)$num != $data) 
 292                     trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR
); 
 294                 for ($i=0; $i<$num; $i++
) 
 295                     $result[] =& $this->_get_value(); 
 298                 return $this->_get_value($c . $data); 
 302     function &_get_value($data=null) { 
 304             $data =& trim($this->_read()); 
 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); 
 315             trigger_error("Unkown response prefix 
for '$c$data'", E_USER_ERROR); 
 318             $data =& $this->_read(); 
 324         return substr($buffer, 0, -2); 
 330 //var_dump($r->info());