updated redisent to make it redis 2.2 compatible

This commit is contained in:
Oleg Topchiy 2011-02-25 21:41:54 +02:00
parent 4bc96dd88c
commit d39a5f57d6

View File

@ -28,15 +28,18 @@ class Redisent {
private $__sock;
/**
* Redis bulk commands, they are sent in a slightly different format to the server
* @var array
* @access private
* Host of the Redis server
* @var string
* @access public
*/
private $bulk_cmds = array(
'SET', 'GETSET', 'SETNX', 'ECHO',
'RPUSH', 'LPUSH', 'LSET', 'LREM',
'SADD', 'SREM', 'SMOVE', 'SISMEMBER'
);
public $host;
/**
* Port on which the Redis server is running
* @var integer
* @access public
*/
public $port;
/**
* Creates a Redisent connection to the Redis server on host {@link $host} and port {@link $port}.
@ -44,9 +47,11 @@ class Redisent {
* @param integer $port The port number of the Redis server
*/
function __construct($host, $port = 6379) {
$this->__sock = fsockopen($host, $port, $errno, $errstr);
$this->host = $host;
$this->port = $port;
$this->__sock = fsockopen($this->host, $this->port, $errno, $errstr);
if (!$this->__sock) {
throw new Exception("{$errno} - {$errstr}");
throw new \Exception("{$errno} - {$errstr}");
}
}
@ -56,25 +61,25 @@ class Redisent {
function __call($name, $args) {
/* Build the Redis protocol command */
$name = strtoupper($name);
if (in_array($name, $this->bulk_cmds)) {
$value = array_pop($args);
$command = sprintf("%s %s %d%s%s%s", $name, trim(implode(' ', $args)), strlen($value), CRLF, $value, CRLF);
}
else {
$command = sprintf("%s %s%s", $name, trim(implode(' ', $args)), CRLF);
}
/* Build the Redis unified protocol command */
array_unshift($args, strtoupper($name));
$command = sprintf('*%d%s%s%s', count($args), CRLF, implode(array_map(function($arg) {
return sprintf('$%d%s%s', strlen($arg), CRLF, $arg);
}, $args), CRLF), CRLF);
/* Open a Redis connection and execute the command */
fwrite($this->__sock, $command);
for ($written = 0; $written < strlen($command); $written += $fwrite) {
$fwrite = fwrite($this->__sock, substr($command, $written));
if ($fwrite === FALSE) {
throw new \Exception('Failed to write entire command to stream');
}
}
/* Parse the response based on the reply identifier */
$reply = trim(fgets($this->__sock, 512));
switch (substr($reply, 0, 1)) {
/* Error reply */
case '-':
echo $command."\n";
throw new RedisException(substr(trim($reply), 4));
break;
/* Inline reply */
@ -124,7 +129,7 @@ class Redisent {
break;
/* Integer reply */
case ':':
$response = substr(trim($reply), 1);
$response = intval(substr(trim($reply), 1));
break;
default:
throw new RedisException("invalid server response: {$reply}");