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