X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/ed9b544e10b84cd43348ddfab7068b610a5df1f7..3c910136c1050ce3943c63201eedb7e940652f38:/client-libraries/php/redis.php diff --git a/client-libraries/php/redis.php b/client-libraries/php/redis.php index 7d03ba31..643a3ecb 100644 --- a/client-libraries/php/redis.php +++ b/client-libraries/php/redis.php @@ -19,7 +19,7 @@ class Redis { var $port; var $_sock; - function Redis($host, $port=6379) { + function Redis($host='localhost', $port=6379) { $this->host = $host; $this->port = $port; } @@ -46,7 +46,7 @@ class Redis { function &ping() { $this->connect(); $this->_write("PING\r\n"); - return $this->_simple_response(); + return $this->get_response(); } function &do_echo($s) { @@ -61,7 +61,7 @@ class Redis { ($preserve ? 'SETNX' : 'SET') . " $name " . strlen($value) . "\r\n$value\r\n" ); - return $preserve ? $this->_numeric_response() : $this->_simple_response(); + return $this->get_response(); } function &get($name) { @@ -76,7 +76,7 @@ class Redis { $this->_write("INCR $name\r\n"); else $this->_write("INCRBY $name $amount\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &decr($name, $amount=1) { @@ -85,19 +85,19 @@ class Redis { $this->_write("DECR $name\r\n"); else $this->_write("DECRBY $name $amount\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &exists($name) { $this->connect(); $this->_write("EXISTS $name\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &delete($name) { $this->connect(); $this->_write("DEL $name\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &keys($pattern) { @@ -109,19 +109,19 @@ class Redis { function &randomkey() { $this->connect(); $this->_write("RANDOMKEY\r\n"); - $s =& trim($this->_read()); - $this->_check_for_error($s); - return $s; + return $this->get_response(); } function &rename($src, $dst, $preserve=False) { $this->connect(); - if ($preserve) { - $this->_write("RENAMENX $src $dst\r\n"); - return $this->_numeric_response(); - } - $this->_write("RENAME $src $dst\r\n"); - return trim($this->_simple_response()); + $this->_write($preserve ? "RENAMENX $src $dst\r\n" : "RENAME $src $dst\r\n"); + return $this->get_response(); + } + + function &expire($name, $time) { + $this->connect(); + $this->_write("EXPIRE $name $time\r\n"); + return $this->get_response(); } function &push($name, $value, $tail=true) { @@ -131,13 +131,13 @@ class Redis { ($tail ? 'RPUSH' : 'LPUSH') . " $name " . strlen($value) . "\r\n$value\r\n" ); - return $this->_simple_response(); + return $this->get_response(); } function <rim($name, $start, $end) { $this->connect(); $this->_write("LTRIM $name $start $end\r\n"); - return $this->_simple_response(); + return $this->get_response(); } function &lindex($name, $index) { @@ -158,95 +158,112 @@ class Redis { function &llen($name) { $this->connect(); $this->_write("LLEN $name\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &lrange($name, $start, $end) { $this->connect(); $this->_write("LRANGE $name $start $end\r\n"); - return $this->_get_multi(); + return $this->get_response(); } function &sort($name, $query=false) { $this->connect(); - if ($query === false) { - $this->_write("SORT $name\r\n"); - } else { - $this->_write("SORT $name $query\r\n"); - } - return $this->_get_multi(); + $this->_write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n"); + return $this->get_response(); } function &lset($name, $value, $index) { $this->connect(); $this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n"); - return $this->_simple_response(); + return $this->get_response(); } function &sadd($name, $value) { $this->connect(); $this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &srem($name, $value) { $this->connect(); $this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &sismember($name, $value) { $this->connect(); $this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &sinter($sets) { $this->connect(); $this->_write('SINTER ' . implode(' ', $sets) . "\r\n"); - return $this->_get_multi(); + return $this->get_response(); } function &smembers($name) { $this->connect(); $this->_write("SMEMBERS $name\r\n"); - return $this->_get_multi(); + return $this->get_response(); } function &scard($name) { $this->connect(); $this->_write("SCARD $name\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &select_db($name) { $this->connect(); $this->_write("SELECT $name\r\n"); - return $this->_simple_response(); + return $this->get_response(); } function &move($name, $db) { $this->connect(); $this->_write("MOVE $name $db\r\n"); - return $this->_numeric_response(); + return $this->get_response(); } function &save($background=false) { $this->connect(); $this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n")); - return $this->_simple_response(); + return $this->get_response(); } function &lastsave() { $this->connect(); $this->_write("LASTSAVE\r\n"); - return $this->_numeric_response(); + return $this->get_response(); + } + + function &flush($all=false) { + $this->connect(); + $this->_write($all ? "FLUSH\r\n" : "FLUSHDB\r\n"); + return $this->get_response(); + } + + function &info() { + $this->connect(); + $this->_write("INFO\r\n"); + $info = array(); + $data =& $this->get_response(); + foreach (explode("\r\n", $data) as $l) { + if (!$l) + continue; + list($k, $v) = explode(':', $l, 2); + $_v = strpos($v, '.') !== false ? (float)$v : (int)$v; + $info[$k] = (string)$_v == $v ? $_v : $v; + } + return $info; } function &_write($s) { while ($s) { $i = fwrite($this->_sock, $s); - if ($i == 0) + if ($i == 0) // || $i == strlen($s)) break; $s = substr($s, $i); } @@ -259,72 +276,57 @@ class Redis { trigger_error("Cannot read from socket.", E_USER_ERROR); } - function _check_for_error(&$s) { - if (!$s || $s[0] != '-') - return; - if (substr($s, 0, 4) == '-ERR') - trigger_error("Redis error: " . trim(substr($s, 4)), E_USER_ERROR); - trigger_error("Redis error: " . substr(trim($this->_read()), 5), E_USER_ERROR); - } - - function &_simple_response() { - $s =& trim($this->_read()); - if ($s[0] == '+') - return substr($s, 1); - if ($err =& $this->_check_for_error($s)) - return $err; - trigger_error("Cannot parse first line '$s' for a simple response", E_USER_ERROR); - } - - function &_numeric_response($allow_negative=True) { - $s =& trim($this->_read()); - $i = (int)$s; - if ($i . '' == $s) { - if (!$allow_negative && $i < 0) - $this->_check_for_error($s); - return $i; + function &get_response() { + $data = trim($this->_read()); + $c = $data[0]; + $data = substr($data, 1); + switch ($c) { + case '-': + trigger_error(substr($data, 0, 4) == 'ERR ' ? substr($data, 4) : $data, E_USER_ERROR); + break; + case '+': + return $data; + case '*': + $num = (int)$data; + if ((string)$num != $data) + trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR); + $result = array(); + for ($i=0; $i<$num; $i++) + $result[] =& $this->_get_value(); + return $result; + default: + return $this->_get_value($c . $data); } - if ($s == 'nil') + } + + function &_get_value($data=null) { + if ($data === null) + $data =& trim($this->_read()); + if ($data == '$-1') return null; - trigger_error("Cannot parse '$s' as numeric response."); - } - - function &_get_value() { - $s =& trim($this->_read()); - if ($s == 'nil') - return ''; - else if ($s[0] == '-') - $this->_check_for_error($s); - $i = (int)$s; - if ($i . '' != $s) - trigger_error("Cannot parse '$s' as data length."); + $c = $data[0]; + $data = substr($data, 1); + $i = strpos($data, '.') !== false ? (int)$data : (float)$data; + if ((string)$i != $data) + trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR); + if ($c == ':') + return $i; + if ($c != '$') + trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR); $buffer = ''; - while ($i > 0) { - $s = $this->_read(); - $l = strlen($s); - $i -= $l; - if ($l > $i) // ending crlf - $s = rtrim($s); - $buffer .= $s; - } - if ($i == 0) // let's restore the trailing crlf - $buffer .= $this->_read(); - return $buffer; - } - - function &_get_multi() { - $results = array(); - $num =& $this->_numeric_response(false); - if ($num === false) - return $results; - while ($num) { - $results[] =& $this->_get_value(); - $num -= 1; + while (true) { + $data =& $this->_read(); + $i -= strlen($data); + $buffer .= $data; + if ($i < 0) + break; } - return $results; + return substr($buffer, 0, -2); } } +//$r =& new Redis(); +//var_dump($r->info()); ?>