]>
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 mget($keys) { 
  74         $this->write("MGET ".implode(" ",$keys)."\r\n"); 
  75         return $this->get_response(); 
  78     public function incr($name, $amount=1) { 
  81             $this->write("INCR $name\r\n"); 
  83             $this->write("INCRBY $name $amount\r\n"); 
  84         return $this->get_response(); 
  87     public function decr($name, $amount=1) { 
  90             $this->write("DECR $name\r\n"); 
  92             $this->write("DECRBY $name $amount\r\n"); 
  93         return $this->get_response(); 
  96     public function exists($name) { 
  98         $this->write("EXISTS $name\r\n"); 
  99         return $this->get_response(); 
 102     public function delete($name) { 
 104         $this->write("DEL $name\r\n"); 
 105         return $this->get_response(); 
 108     public function keys($pattern) { 
 110         $this->write("KEYS $pattern\r\n"); 
 111         return explode(' ', $this->get_response()); 
 114     public function randomkey() { 
 116         $this->write("RANDOMKEY\r\n"); 
 117         return $this->get_response(); 
 120     public function rename($src, $dst) { 
 122         $this->write("RENAME $src $dst\r\n"); 
 123         return $this->get_response(); 
 126     public function renamenx($src, $dst) { 
 128         $this->write("RENAMENX $src $dst\r\n"); 
 129         return $this->get_response(); 
 132     public function expire($name, $time) { 
 134         $this->write("EXPIRE $name $time\r\n"); 
 135         return $this->get_response(); 
 138     public function push($name, $value, $tail=true) { 
 139         // default is to append the element to the list 
 142             ($tail ? 'RPUSH' : 'LPUSH') . 
 143             " $name " . strlen($value) . "\r\n$value\r\n" 
 145         return $this->get_response(); 
 148     public function lpush($name, $value) { 
 149         return $this->push($name, $value, false); 
 152     public function rpush($name, $value) { 
 153         return $this->push($name, $value, true); 
 156     public function ltrim($name, $start, $end) { 
 158         $this->write("LTRIM $name $start $end\r\n"); 
 159         return $this->get_response(); 
 162     public function lindex($name, $index) { 
 164         $this->write("LINDEX $name $index\r\n"); 
 165         return $this->get_response(); 
 168     public function pop($name, $tail=true) { 
 171             ($tail ? 'RPOP' : 'LPOP') . 
 174         return $this->get_response(); 
 177     public function lpop($name, $value) { 
 178         return $this->pop($name, $value, false); 
 181     public function rpop($name, $value) { 
 182         return $this->pop($name, $value, true); 
 185     public function llen($name) { 
 187         $this->write("LLEN $name\r\n"); 
 188         return $this->get_response(); 
 191     public function lrange($name, $start, $end) { 
 193         $this->write("LRANGE $name $start $end\r\n"); 
 194         return $this->get_response(); 
 197     public function sort($name, $query=false) { 
 199         $this->write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n"); 
 200         return $this->get_response(); 
 203     public function lset($name, $value, $index) { 
 205         $this->write("LSET $name $index " . strlen($value) . "\r\n$value\r\n"); 
 206         return $this->get_response(); 
 209     public function sadd($name, $value) { 
 211         $this->write("SADD $name " . strlen($value) . "\r\n$value\r\n"); 
 212         return $this->get_response(); 
 215     public function srem($name, $value) { 
 217         $this->write("SREM $name " . strlen($value) . "\r\n$value\r\n"); 
 218         return $this->get_response(); 
 221     public function sismember($name, $value) { 
 223         $this->write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n"); 
 224         return $this->get_response(); 
 227     public function sinter($sets) { 
 229         $this->write('SINTER ' . implode(' ', $sets) . "\r\n"); 
 230         return $this->get_response(); 
 233     public function smembers($name) { 
 235         $this->write("SMEMBERS $name\r\n"); 
 236         return $this->get_response(); 
 239     public function scard($name) { 
 241         $this->write("SCARD $name\r\n"); 
 242         return $this->get_response(); 
 245     public function select_db($name) { 
 247         $this->write("SELECT $name\r\n"); 
 248         return $this->get_response(); 
 251     public function move($name, $db) { 
 253         $this->write("MOVE $name $db\r\n"); 
 254         return $this->get_response(); 
 257     public function save($background=false) { 
 259         $this->write(($background ? "BGSAVE\r\n" : "SAVE\r\n")); 
 260         return $this->get_response(); 
 263     public function bgsave($background=false) { 
 264         return $this->save(true); 
 267     public function lastsave() { 
 269         $this->write("LASTSAVE\r\n"); 
 270         return $this->get_response(); 
 273     public function flushdb($all=false) { 
 275         $this->write($all ? "FLUSHALL\r\n" : "FLUSHDB\r\n"); 
 276         return $this->get_response(); 
 279     public function flushall() { 
 280         return $this->flush(true); 
 283     public function info() { 
 285         $this->write("INFO\r\n"); 
 287         $data =& $this->get_response(); 
 288         foreach (explode("\r\n", $data) as $l) { 
 291             list($k, $v) = explode(':', $l, 2); 
 292             $_v = strpos($v, '.') !== false ? (float)$v : (int)$v; 
 293             $info[$k] = (string)$_v == $v ? $_v : $v; 
 298     private function write($s) { 
 300             $i = fwrite($this->_sock
, $s); 
 301             if ($i == 0) // || $i == strlen($s)) 
 307     private function read($len=1024) { 
 308         if ($s = fgets($this->_sock
)) 
 311         trigger_error("Cannot read from socket.", E_USER_ERROR
); 
 314     private function get_response() { 
 315         $data = trim($this->read()); 
 317         $data = substr($data, 1); 
 320                 trigger_error($data, E_USER_ERROR
); 
 325                 $i = strpos($data, '.') !== false ? (int)$data : (float)$data; 
 326                 if ((string)$i != $data) 
 327                     trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR
); 
 330                 return $this->get_bulk_reply($c . $data); 
 333                 if ((string)$num != $data) 
 334                     trigger_error("Cannot convert multi-response header '$data' to 
integer", E_USER_ERROR); 
 336                 for ($i=0; $i<$num; $i++) 
 337                     $result[] =& $this->get_response(); 
 340                 trigger_error("Invalid reply type byte
: '$c'"); 
 344     private function get_bulk_reply($data=null) { 
 346             $data = trim($this->read()); 
 350         $data = substr($data, 1); 
 351         $bulklen = (int)$data; 
 352         if ((string)$bulklen != $data) 
 353             trigger_error("Cannot convert bulk read header '$c$data' to integer", E_USER_ERROR
); 
 355             trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR
); 
 358             $data = fread($this->_sock
,$bulklen); 
 359             $bulklen -= strlen($data); 
 362         $crlf = fread($this->_sock
,2); 
 369 var_dump($r->set("foo","bar")); 
 370 var_dump($r->get("foo")); 
 371 var_dump($r->info());