redict/client-libraries/php/redis.php

375 lines
10 KiB
PHP
Raw Normal View History

2009-03-22 05:30:00 -04:00
<?php
/*******************************************************************************
* Redis PHP Bindings - http://code.google.com/p/redis/
*
* Copyright 2009 Ludovico Magnocavallo
2009-04-21 13:43:02 -04:00
* Copyright 2009 Salvatore Sanfilippo (ported it to PHP5, fixed some bug)
2009-03-22 05:30:00 -04:00
* Released under the same license as Redis.
*
* Version: 0.1
*
* $Revision: 139 $
* $Date: 2009-03-15 22:59:40 +0100 (Dom, 15 Mar 2009) $
*
******************************************************************************/
class Redis {
2009-04-21 13:43:02 -04:00
public $server;
public $port;
private $_sock;
public function __construct($host='localhost', $port=6379) {
2009-03-22 05:30:00 -04:00
$this->host = $host;
$this->port = $port;
}
2009-04-21 13:43:02 -04:00
public function connect() {
if ($this->_sock) return;
2009-03-22 05:30:00 -04:00
if ($sock = fsockopen($this->host, $this->port, $errno, $errstr)) {
$this->_sock = $sock;
return;
}
$msg = "Cannot open socket to {$this->host}:{$this->port}";
if ($errno || $errmsg)
2009-04-21 13:43:02 -04:00
$msg .= "," . ($errno ? " error $errno" : "") .
($errmsg ? " $errmsg" : "");
2009-03-22 05:30:00 -04:00
trigger_error("$msg.", E_USER_ERROR);
}
2009-04-21 13:43:02 -04:00
public function disconnect() {
if ($this->_sock) @fclose($this->_sock);
2009-03-22 05:30:00 -04:00
$this->_sock = null;
}
2009-04-21 13:43:02 -04:00
public function ping() {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("PING\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function do_echo($s) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("ECHO " . strlen($s) . "\r\n$s\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function set($name, $value, $preserve=false) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write(
2009-03-22 05:30:00 -04:00
($preserve ? 'SETNX' : 'SET') .
" $name " . strlen($value) . "\r\n$value\r\n"
);
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function get($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("GET $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
public function mget($keys) {
$this->connect();
$this->write("MGET ".implode(" ",$keys)."\r\n");
return $this->get_response();
}
2009-03-22 05:30:00 -04:00
2009-04-21 13:43:02 -04:00
public function incr($name, $amount=1) {
2009-03-22 05:30:00 -04:00
$this->connect();
if ($amount == 1)
2009-04-21 13:43:02 -04:00
$this->write("INCR $name\r\n");
2009-03-22 05:30:00 -04:00
else
2009-04-21 13:43:02 -04:00
$this->write("INCRBY $name $amount\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function decr($name, $amount=1) {
2009-03-22 05:30:00 -04:00
$this->connect();
if ($amount == 1)
2009-04-21 13:43:02 -04:00
$this->write("DECR $name\r\n");
2009-03-22 05:30:00 -04:00
else
2009-04-21 13:43:02 -04:00
$this->write("DECRBY $name $amount\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function exists($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("EXISTS $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function delete($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("DEL $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function keys($pattern) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("KEYS $pattern\r\n");
return explode(' ', $this->get_response());
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function randomkey() {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("RANDOMKEY\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function rename($src, $dst) {
$this->connect();
$this->write("RENAME $src $dst\r\n");
return $this->get_response();
}
public function renamenx($src, $dst) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("RENAMENX $src $dst\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function expire($name, $time) {
2009-04-08 06:09:56 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("EXPIRE $name $time\r\n");
2009-04-08 06:09:56 -04:00
return $this->get_response();
}
2009-04-21 13:43:02 -04:00
public function push($name, $value, $tail=true) {
2009-03-22 05:30:00 -04:00
// default is to append the element to the list
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write(
2009-03-22 05:30:00 -04:00
($tail ? 'RPUSH' : 'LPUSH') .
" $name " . strlen($value) . "\r\n$value\r\n"
);
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function lpush($name, $value) {
return $this->push($name, $value, false);
}
public function rpush($name, $value) {
return $this->push($name, $value, true);
}
public function ltrim($name, $start, $end) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("LTRIM $name $start $end\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function lindex($name, $index) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("LINDEX $name $index\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function pop($name, $tail=true) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write(
2009-03-22 05:30:00 -04:00
($tail ? 'RPOP' : 'LPOP') .
" $name\r\n"
);
2009-04-21 13:43:02 -04:00
return $this->get_response();
}
public function lpop($name, $value) {
return $this->pop($name, $value, false);
}
public function rpop($name, $value) {
return $this->pop($name, $value, true);
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function llen($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("LLEN $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function lrange($name, $start, $end) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("LRANGE $name $start $end\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function sort($name, $query=false) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function lset($name, $value, $index) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function sadd($name, $value) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("SADD $name " . strlen($value) . "\r\n$value\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function srem($name, $value) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("SREM $name " . strlen($value) . "\r\n$value\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function sismember($name, $value) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function sinter($sets) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write('SINTER ' . implode(' ', $sets) . "\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function smembers($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("SMEMBERS $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function scard($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("SCARD $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function select_db($name) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("SELECT $name\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function move($name, $db) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("MOVE $name $db\r\n");
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function save($background=false) {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
return $this->get_response();
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
public function bgsave($background=false) {
return $this->save(true);
}
public function lastsave() {
2009-03-22 05:30:00 -04:00
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write("LASTSAVE\r\n");
return $this->get_response();
}
2009-04-21 13:43:02 -04:00
public function flushdb($all=false) {
$this->connect();
2009-04-21 13:43:02 -04:00
$this->write($all ? "FLUSHALL\r\n" : "FLUSHDB\r\n");
return $this->get_response();
}
2009-04-21 13:43:02 -04:00
public function flushall() {
return $this->flush(true);
}
2009-04-21 13:43:02 -04:00
public function info() {
$this->connect();
2009-04-21 13:43:02 -04:00
$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;
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
private function write($s) {
2009-03-22 05:30:00 -04:00
while ($s) {
$i = fwrite($this->_sock, $s);
if ($i == 0) // || $i == strlen($s))
2009-03-22 05:30:00 -04:00
break;
$s = substr($s, $i);
}
}
2009-04-21 13:43:02 -04:00
private function read($len=1024) {
2009-03-22 05:30:00 -04:00
if ($s = fgets($this->_sock))
return $s;
$this->disconnect();
trigger_error("Cannot read from socket.", E_USER_ERROR);
}
2009-04-21 13:43:02 -04:00
private function get_response() {
$data = trim($this->read());
$c = $data[0];
$data = substr($data, 1);
switch ($c) {
case '-':
2009-04-21 13:43:02 -04:00
trigger_error($data, E_USER_ERROR);
break;
case '+':
return $data;
2009-04-21 13:43:02 -04:00
case ':':
$i = strpos($data, '.') !== false ? (int)$data : (float)$data;
if ((string)$i != $data)
trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR);
return $i;
case '$':
return $this->get_bulk_reply($c . $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++)
2009-04-21 13:43:02 -04:00
$result[] =& $this->get_response();
return $result;
default:
2009-04-21 13:43:02 -04:00
trigger_error("Invalid reply type byte: '$c'");
2009-03-22 05:30:00 -04:00
}
}
2009-04-21 13:43:02 -04:00
private function get_bulk_reply($data=null) {
if ($data === null)
2009-04-21 13:43:02 -04:00
$data = trim($this->read());
if ($data == '$-1')
2009-03-22 05:30:00 -04:00
return null;
$c = $data[0];
$data = substr($data, 1);
2009-04-21 13:43:02 -04:00
$bulklen = (int)$data;
if ((string)$bulklen != $data)
trigger_error("Cannot convert bulk read header '$c$data' to integer", E_USER_ERROR);
if ($c != '$')
trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR);
2009-03-22 05:30:00 -04:00
$buffer = '';
2009-04-21 13:43:02 -04:00
while ($bulklen) {
$data = fread($this->_sock,$bulklen);
$bulklen -= strlen($data);
$buffer .= $data;
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
$crlf = fread($this->_sock,2);
return $buffer;
2009-03-22 05:30:00 -04:00
}
2009-04-21 13:43:02 -04:00
}
2009-03-22 05:30:00 -04:00
/*
2009-04-21 13:43:02 -04:00
$r = new Redis();
var_dump($r->set("foo","bar"));
2009-04-21 13:43:02 -04:00
var_dump($r->get("foo"));
var_dump($r->info());
*/
2009-03-22 05:30:00 -04:00
?>