mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 09:00:51 +00:00
fix decode bug, add flush and info commands
This commit is contained in:
parent
ce7bef0795
commit
bb7dcc1e51
@ -19,7 +19,7 @@ class Redis {
|
|||||||
var $port;
|
var $port;
|
||||||
var $_sock;
|
var $_sock;
|
||||||
|
|
||||||
function Redis($host, $port=6379) {
|
function Redis($host='localhost', $port=6379) {
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ class Redis {
|
|||||||
function &ping() {
|
function &ping() {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("PING\r\n");
|
$this->_write("PING\r\n");
|
||||||
return $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &do_echo($s) {
|
function &do_echo($s) {
|
||||||
@ -61,7 +61,7 @@ class Redis {
|
|||||||
($preserve ? 'SETNX' : 'SET') .
|
($preserve ? 'SETNX' : 'SET') .
|
||||||
" $name " . strlen($value) . "\r\n$value\r\n"
|
" $name " . strlen($value) . "\r\n$value\r\n"
|
||||||
);
|
);
|
||||||
return $preserve ? $this->_numeric_response() : $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &get($name) {
|
function &get($name) {
|
||||||
@ -76,7 +76,7 @@ class Redis {
|
|||||||
$this->_write("INCR $name\r\n");
|
$this->_write("INCR $name\r\n");
|
||||||
else
|
else
|
||||||
$this->_write("INCRBY $name $amount\r\n");
|
$this->_write("INCRBY $name $amount\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &decr($name, $amount=1) {
|
function &decr($name, $amount=1) {
|
||||||
@ -85,19 +85,19 @@ class Redis {
|
|||||||
$this->_write("DECR $name\r\n");
|
$this->_write("DECR $name\r\n");
|
||||||
else
|
else
|
||||||
$this->_write("DECRBY $name $amount\r\n");
|
$this->_write("DECRBY $name $amount\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &exists($name) {
|
function &exists($name) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("EXISTS $name\r\n");
|
$this->_write("EXISTS $name\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &delete($name) {
|
function &delete($name) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("DEL $name\r\n");
|
$this->_write("DEL $name\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &keys($pattern) {
|
function &keys($pattern) {
|
||||||
@ -109,19 +109,13 @@ class Redis {
|
|||||||
function &randomkey() {
|
function &randomkey() {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("RANDOMKEY\r\n");
|
$this->_write("RANDOMKEY\r\n");
|
||||||
$s =& trim($this->_read());
|
return $this->get_response();
|
||||||
$this->_check_for_error($s);
|
|
||||||
return $s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function &rename($src, $dst, $preserve=False) {
|
function &rename($src, $dst, $preserve=False) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
if ($preserve) {
|
$this->_write($preserve ? "RENAMENX $src $dst\r\n" : "RENAME $src $dst\r\n");
|
||||||
$this->_write("RENAMENX $src $dst\r\n");
|
return $this->get_response();
|
||||||
return $this->_numeric_response();
|
|
||||||
}
|
|
||||||
$this->_write("RENAME $src $dst\r\n");
|
|
||||||
return trim($this->_simple_response());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function &push($name, $value, $tail=true) {
|
function &push($name, $value, $tail=true) {
|
||||||
@ -131,13 +125,13 @@ class Redis {
|
|||||||
($tail ? 'RPUSH' : 'LPUSH') .
|
($tail ? 'RPUSH' : 'LPUSH') .
|
||||||
" $name " . strlen($value) . "\r\n$value\r\n"
|
" $name " . strlen($value) . "\r\n$value\r\n"
|
||||||
);
|
);
|
||||||
return $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function <rim($name, $start, $end) {
|
function <rim($name, $start, $end) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("LTRIM $name $start $end\r\n");
|
$this->_write("LTRIM $name $start $end\r\n");
|
||||||
return $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &lindex($name, $index) {
|
function &lindex($name, $index) {
|
||||||
@ -158,95 +152,112 @@ class Redis {
|
|||||||
function &llen($name) {
|
function &llen($name) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("LLEN $name\r\n");
|
$this->_write("LLEN $name\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &lrange($name, $start, $end) {
|
function &lrange($name, $start, $end) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("LRANGE $name $start $end\r\n");
|
$this->_write("LRANGE $name $start $end\r\n");
|
||||||
return $this->_get_multi();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &sort($name, $query=false) {
|
function &sort($name, $query=false) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
if ($query === false) {
|
$this->_write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n");
|
||||||
$this->_write("SORT $name\r\n");
|
return $this->get_response();
|
||||||
} else {
|
|
||||||
$this->_write("SORT $name $query\r\n");
|
|
||||||
}
|
|
||||||
return $this->_get_multi();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function &lset($name, $value, $index) {
|
function &lset($name, $value, $index) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
|
$this->_write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
|
||||||
return $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &sadd($name, $value) {
|
function &sadd($name, $value) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n");
|
$this->_write("SADD $name " . strlen($value) . "\r\n$value\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &srem($name, $value) {
|
function &srem($name, $value) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n");
|
$this->_write("SREM $name " . strlen($value) . "\r\n$value\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &sismember($name, $value) {
|
function &sismember($name, $value) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
|
$this->_write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &sinter($sets) {
|
function &sinter($sets) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write('SINTER ' . implode(' ', $sets) . "\r\n");
|
$this->_write('SINTER ' . implode(' ', $sets) . "\r\n");
|
||||||
return $this->_get_multi();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &smembers($name) {
|
function &smembers($name) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("SMEMBERS $name\r\n");
|
$this->_write("SMEMBERS $name\r\n");
|
||||||
return $this->_get_multi();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &scard($name) {
|
function &scard($name) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("SCARD $name\r\n");
|
$this->_write("SCARD $name\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &select_db($name) {
|
function &select_db($name) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("SELECT $name\r\n");
|
$this->_write("SELECT $name\r\n");
|
||||||
return $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &move($name, $db) {
|
function &move($name, $db) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("MOVE $name $db\r\n");
|
$this->_write("MOVE $name $db\r\n");
|
||||||
return $this->_numeric_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &save($background=false) {
|
function &save($background=false) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
|
$this->_write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
|
||||||
return $this->_simple_response();
|
return $this->get_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
function &lastsave() {
|
function &lastsave() {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$this->_write("LASTSAVE\r\n");
|
$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) {
|
function &_write($s) {
|
||||||
while ($s) {
|
while ($s) {
|
||||||
$i = fwrite($this->_sock, $s);
|
$i = fwrite($this->_sock, $s);
|
||||||
if ($i == 0)
|
if ($i == 0) // || $i == strlen($s))
|
||||||
break;
|
break;
|
||||||
$s = substr($s, $i);
|
$s = substr($s, $i);
|
||||||
}
|
}
|
||||||
@ -259,72 +270,57 @@ class Redis {
|
|||||||
trigger_error("Cannot read from socket.", E_USER_ERROR);
|
trigger_error("Cannot read from socket.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _check_for_error(&$s) {
|
function &get_response() {
|
||||||
if (!$s || $s[0] != '-')
|
$data = trim($this->_read());
|
||||||
return;
|
$c = $data[0];
|
||||||
if (substr($s, 0, 4) == '-ERR')
|
$data = substr($data, 1);
|
||||||
trigger_error("Redis error: " . trim(substr($s, 4)), E_USER_ERROR);
|
switch ($c) {
|
||||||
trigger_error("Redis error: " . substr(trim($this->_read()), 5), E_USER_ERROR);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function &_simple_response() {
|
function &_get_value($data=null) {
|
||||||
$s =& trim($this->_read());
|
if ($data === null)
|
||||||
if ($s[0] == '+')
|
$data =& trim($this->_read());
|
||||||
return substr($s, 1);
|
if ($data == '$-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;
|
|
||||||
}
|
|
||||||
if ($s == 'nil')
|
|
||||||
return null;
|
return null;
|
||||||
trigger_error("Cannot parse '$s' as numeric response.");
|
$c = $data[0];
|
||||||
}
|
$data = substr($data, 1);
|
||||||
|
$i = strpos($data, '.') !== false ? (int)$data : (float)$data;
|
||||||
function &_get_value() {
|
if ((string)$i != $data)
|
||||||
$s =& trim($this->_read());
|
trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR);
|
||||||
if ($s == 'nil')
|
if ($c == ':')
|
||||||
return '';
|
return $i;
|
||||||
else if ($s[0] == '-')
|
if ($c != '$')
|
||||||
$this->_check_for_error($s);
|
trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR);
|
||||||
$i = (int)$s;
|
|
||||||
if ($i . '' != $s)
|
|
||||||
trigger_error("Cannot parse '$s' as data length.");
|
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
while ($i > 0) {
|
while (true) {
|
||||||
$s = $this->_read();
|
$data =& $this->_read();
|
||||||
$l = strlen($s);
|
$i -= strlen($data);
|
||||||
$i -= $l;
|
$buffer .= $data;
|
||||||
if ($l > $i) // ending crlf
|
if ($i < 0)
|
||||||
$s = rtrim($s);
|
break;
|
||||||
$buffer .= $s;
|
|
||||||
}
|
}
|
||||||
if ($i == 0) // let's restore the trailing crlf
|
return substr($buffer, 0, -2);
|
||||||
$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;
|
|
||||||
}
|
|
||||||
return $results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//$r =& new Redis();
|
||||||
|
//var_dump($r->info());
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@ -6,6 +6,8 @@ require_once('redis.php');
|
|||||||
|
|
||||||
$r =& new Redis('localhost');
|
$r =& new Redis('localhost');
|
||||||
$r->connect();
|
$r->connect();
|
||||||
|
$r->select_db(9);
|
||||||
|
$r->flush();
|
||||||
echo $r->ping() . "\n";
|
echo $r->ping() . "\n";
|
||||||
echo $r->do_echo('ECHO test') . "\n";
|
echo $r->do_echo('ECHO test') . "\n";
|
||||||
echo "SET aaa " . $r->set('aaa', 'bbb') . "\n";
|
echo "SET aaa " . $r->set('aaa', 'bbb') . "\n";
|
||||||
@ -61,18 +63,24 @@ echo 'SREM s0 bbb ' . $r->srem('s0', 'bbb') . "\n";
|
|||||||
echo 'SINTER s0 s1 ' . print_r($r->sinter(array('s0', 's1')), true) . "\n";
|
echo 'SINTER s0 s1 ' . print_r($r->sinter(array('s0', 's1')), true) . "\n";
|
||||||
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
||||||
|
|
||||||
echo 'SELECT 1 ' . $r->select_db(1) . "\n";
|
echo 'SELECT 8 ' . $r->select_db(8) . "\n";
|
||||||
|
echo 'EXISTS s1 ' . $r->exists('s1') . "\n";
|
||||||
|
if ($r->exists('s1'))
|
||||||
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
||||||
echo 'SELECT 0 ' . $r->select_db(0) . "\n";
|
echo 'SELECT 9 ' . $r->select_db(9) . "\n";
|
||||||
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
||||||
echo 'MOVE s1 1 ' . $r->move('s1', 1) . "\n";
|
echo 'MOVE s1 8 ' . $r->move('s1', 8) . "\n";
|
||||||
|
echo 'EXISTS s1 ' . $r->exists('s1') . "\n";
|
||||||
|
if ($r->exists('s1'))
|
||||||
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
||||||
echo 'SELECT 1 ' . $r->select_db(1) . "\n";
|
echo 'SELECT 8 ' . $r->select_db(8) . "\n";
|
||||||
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
echo 'SMEMBERS s1 ' . print_r($r->smembers('s1'), true) . "\n";
|
||||||
echo 'SELECT 0 ' . $r->select_db(0) . "\n";
|
echo 'SELECT 9 ' . $r->select_db(9) . "\n";
|
||||||
|
|
||||||
echo 'SAVE ' . $r->save() . "\n";
|
echo 'SAVE ' . $r->save() . "\n";
|
||||||
echo 'BGSAVE ' . $r->save(true) . "\n";
|
echo 'BGSAVE ' . $r->save(true) . "\n";
|
||||||
echo 'LASTSAVE ' . $r->lastsave() . "\n";
|
echo 'LASTSAVE ' . $r->lastsave() . "\n";
|
||||||
|
|
||||||
|
echo 'INFO ' . print_r($r->info()) . "\n";
|
||||||
|
|
||||||
?>
|
?>
|
Loading…
x
Reference in New Issue
Block a user