Commits

Tobias Duehr committed d0c9dc1

init

  • Participants

Comments (0)

Files changed (12)

+<?php
+session_start();
+require_once 'lib/Predis.php';
+require_once 'lib/helpers.php';
+
+class App{
+	
+	private $r;
+	private $views = array("KEYS", "GET");
+	private $actions = array("DEL", "PERSIST");
+	private $last_query;
+	private $output;
+	
+	public $redirect;
+	public $view;
+	public $db;
+	
+	function __construct($db_num = 0){
+		$this->r = new Predis_Client(
+			array('host' => '127.0.0.1', 'port' => 6379, 'database' => $db_num)
+		);
+		$this->last_query = $_SESSION["last_query"] ? $_SESSION["last_query"] : "KEYS";
+		$this->output = new stdClass();
+		$this->output->info = $this->r->info();
+		$this->db = $this->output->db = $db_num;
+	}
+	
+	function download($key){
+		header('Content-Type: application/force-download');
+		header("Content-Transfer-Encoding: binary");
+		header("Content-Disposition: attachment; filename=\"$key\";" ); 
+		echo base64_decode($this->r->get($key));
+	}
+	
+	function open($key){
+		switch($this->r->type($key)){
+			case "list":
+				$this->redirect = "LRANGE/" . rawurlencode($key) . '/0/100';
+			break;
+			case "set":
+				$this->redirect = "SMEMBERS/" . rawurlencode($key);
+			break;
+			case "string":
+			default:
+				$this->redirect = "GET/" . rawurlencode($key);
+		}
+	}
+	
+	function __call($func, $args){
+		try { $result = call_user_func_array(array($this->r, strtolower($func)), $args); }
+		catch(Exception $e) {
+			$this->view = "error";
+			$this->output->error = $e->getMessage();
+			return $this->output;
+		}
+		$this->output->func = $func;
+		$this->output->args = $args;
+		switch(strtoupper($func)){
+			case "GET":
+			case "LRANGE":
+			case "SMEMBERS":
+				$this->output->ttl = $this->r->ttl($args[0]);
+				$this->output->is_key = true;
+		}
+		if(!$result){
+			$this->redirect = "KEYS/*";
+			return false;
+		}
+		if(in_array(strtoupper($func), $this->views)){
+			$this->output->result = $result;
+		} elseif(in_array(strtoupper($func), $this->actions)){
+			$this->redirect = $this->last_query;
+		} else {
+			$this->view = "generic";
+			$this->output->result = $result;
+		}
+		return $this->output;
+	}
+
+}
+
+$query = $_GET["q"] ? explode("/", $_GET["q"]) : array(0, "KEYS", "*");
+$db_num = intval(array_shift($query));
+if(!$query) $query = array("KEYS", "*");
+foreach($query as &$q) $q = rawurldecode($q);
+$APP = new App($db_num);
+
+if(is_string($query)) $_ = $APP->$query();
+else $_ = call_user_func_array(array($APP, $query[0]), array_merge(array_slice($query, 1), $_POST));
+
+if($APP->redirect){
+	header("location: ?q=$APP->db/$APP->redirect");
+	exit;
+}
+
+$_SESSION["last_query"] = implode('/', $query);
+
+if($APP->view) include("frontend/$APP->view.phtml");
+else include("frontend/$query[0].phtml");

File frontend/GET.phtml

+<?php require 'left.phtml' ?>
+<h3>Value</h3>
+<?php if(base64_decode($_->result, true) !== false && strlen($_->result) > 1024): ?>
+	<h4>base64 encoded file</h4>
+	<p>
+		<img src="data:image/jpeg;base64,<?php echo $_->result ?>">
+	</p><p>
+		<a href="?q=download/<?php echo rawurlencode($_->args[0]) ?>">Download</a>
+	</p>
+<?php elseif(@($json = json_decode($_->result)) && (strpos($_->result, '{') !== false || strpos($_->result, '[') !== false)): ?>
+	<h4>JSON</h4>	
+	<p class="json"><?php echo printOut($json) ?></p>
+<?php endif ?>
+<textarea cols="60" rows="20"><?php echo $_->result ?></textarea>
+<?php require 'right.phtml' ?>

File frontend/KEYS.phtml

+<?php require 'left.phtml' ?>
+<form method="post" action="?q=KEYS">
+<input type="text" name="s_keys">
+<input type="submit">
+</form>
+<table cellpadding=0 cellspacing=0>
+	<thead>
+		
+	</thead>
+	<tbody>
+<?php foreach($_->result as $key): ?>
+	<tr>
+		<td>
+			<a href="?q=<?php echo $_->db ?>/open/<?php echo rawurlencode($key) ?>"><?php echo $key ?></a>
+		</td>
+		<td>
+			<a href="?q=<?php echo $_->db ?>/PERSIST/<?php echo rawurlencode($key) ?>">PERSIST</a>
+		</td>
+		<td>
+			<a href="?q=<?php echo $_->db ?>/DEL/<?php echo rawurlencode($key) ?>">DEL</a>
+		</td>
+	</tr>
+<?php endforeach ?>
+	</tbody>
+</table>
+<?php require 'right.phtml' ?>

File frontend/app.css

+* {
+
+	font-family: monospace;
+}
+
+html{
+
+}
+
+table { border-collapse: collapse; }
+
+table td{
+	border: 1px solid #999;
+	width: 100%;
+	padding: 2px 5px;
+}
+
+h3{
+	background: #AAA;
+	color: #FFF;
+	padding: 5px;
+}
+
+textarea{
+	width: 100%;
+}
+
+body{
+	background: #FFF;
+	padding: 25px;
+}
+
+#menu{
+	float: left;
+	width: 250px;
+}
+
+#center{
+	padding-left: 260px;
+}
+
+p.json{
+	max-height: 600px;
+	overflow: auto;
+}

File frontend/app.js

+$(function(){
+	
+	$(".show_players").click(function(){
+		var id = $(this).attr("rel");
+		$("#players-" + $(this).attr("rel")).load("app.php?a=players&post_id=" + id);
+	});
+	
+});

File frontend/error.phtml

+<?php require 'left.phtml' ?>
+<h2>ERROR</h2>
+<?php echo printOut($_->error) ?>
+<?php require 'right.phtml' ?>

File frontend/generic.phtml

+<?php require 'left.phtml' ?>
+	<h2>Generic view:</h2>
+	<?php echo printOut($_->result) ?>
+<?php require 'right.phtml' ?>

File frontend/left.phtml

+<!DOCTYPE html>
+<html>
+<head>
+<meta name="keywords" content="" />
+<meta name="description" content="" />
+<meta http-equiv="content-type" content="text/html; charset=utf-8" />
+<title>rediswatch</title>
+<link href="frontend/app.css" rel="stylesheet" type="text/css" media="screen" />
+</head>
+<body>
+	<div id="menu">
+		<h1>rediswatch</h1>
+	<ul>
+	<?php foreach($_->info as $k => $v): ?>
+		<?php if(is_array($v)): ?>
+		<li>
+			<a href="?q=<?php echo substr($k, 2) ?>"><?php echo $k ?> (<?php echo $v["keys"] ?>)</a>
+		</li>
+		<?php endif ?>
+	<?php endforeach ?>
+	</ul>
+	<ul>
+		<li><a href="?q=0/INFO">Serverinfo</a></li>
+	</ul>
+</div>
+<div id="center">
+<h2><span style="color:#AAA">redis&gt;</span><?php echo $_->func ?> <?php if($_->args) echo implode(' ', $_->args) ?></h2>
+<?php if($_->is_key): ?>
+<h3>Key</h3>
+<h4>Expires in <?php echo date('H \H\o\u\r\s i \M\i\n\u\t\e\s s \S\e\c\o\n\d\s', $_->ttl) ?><h4>
+<p>
+<a href="?q=<?php echo $_->db ?>/DEL/<?php echo rawurlencode($_->args[0]) ?>">DEL</a>
+<a href="?q=<?php echo $_->db ?>/PERSIST/<?php echo rawurlencode($_->args[0]) ?>">PERSIST</a>
+<form method="post"">
+	<p>
+	RENAME <input type="text" value="<?php echo $_->args[0] ?>">
+	</p><p>
+	EXPIRE <input type="number" value="3200">
+	</p><p>
+	<input type="submit">
+	</p>
+</form>
+</p>
+<?php endif ?>

File frontend/right.phtml

+</div>
+
+</body>
+</html>
+<?php require_once("app.php"); ?>

File lib/Predis.php

+<?php
+class PredisException extends Exception { }
+
+// Client-side errors
+class Predis_ClientException extends PredisException { }
+
+// Aborted multi/exec
+class Predis_AbortedMultiExec extends PredisException { }
+
+// Server-side errors
+class Predis_ServerException extends PredisException {
+    public function toResponseError() {
+        return new Predis_ResponseError($this->getMessage());
+    }
+}
+
+// Communication errors
+class Predis_CommunicationException extends PredisException {
+    private $_connection;
+
+    public function __construct(Predis_Connection $connection, $message = null, $code = null) {
+        $this->_connection = $connection;
+        parent::__construct($message, $code);
+    }
+
+    public function getConnection() { return $this->_connection; }
+    public function shouldResetConnection() {  return true; }
+}
+
+// Unexpected responses
+class Predis_MalformedServerResponse extends Predis_CommunicationException { }
+
+/* ------------------------------------------------------------------------- */
+
+class Predis_Client {
+    private $_options, $_connection, $_serverProfile, $_responseReader;
+
+    public function __construct($parameters = null, $clientOptions = null) {
+        $this->setupClient($clientOptions !== null ? $clientOptions : new Predis_ClientOptions());
+        $this->setupConnection($parameters);
+    }
+
+    public static function create(/* arguments */) {
+        $argv = func_get_args();
+        $argc = func_num_args();
+
+        $options = null;
+        $lastArg = $argv[$argc-1];
+        if ($argc > 0 && !is_string($lastArg) && ($lastArg instanceof Predis_ClientOptions ||
+            is_subclass_of($lastArg, 'Predis_RedisServerProfile'))) {
+            $options = array_pop($argv);
+            $argc--;
+        }
+
+        if ($argc === 0) {
+            throw new Predis_ClientException('Missing connection parameters');
+        }
+
+        return new Predis_Client($argc === 1 ? $argv[0] : $argv, $options);
+    }
+
+    private static function filterClientOptions($options) {
+        if ($options instanceof Predis_ClientOptions) {
+            return $options;
+        }
+        if (is_array($options)) {
+            return new Predis_ClientOptions($options);
+        }
+        if ($options instanceof Predis_RedisServerProfile) {
+            return new Predis_ClientOptions(array(
+                'profile' => $options
+            ));
+        }
+        if (is_string($options)) {
+            return new Predis_ClientOptions(array(
+                'profile' => Predis_RedisServerProfile::get($options)
+            ));
+        }
+        throw new InvalidArgumentException("Invalid type for client options");
+    }
+
+    private function setupClient($options) {
+        $this->_responseReader = new Predis_ResponseReader();
+        $this->_options = self::filterClientOptions($options);
+
+        $this->setProfile($this->_options->profile);
+        if ($this->_options->iterable_multibulk === true) {
+            $this->_responseReader->setHandler(
+                Predis_Protocol::PREFIX_MULTI_BULK, 
+                new Predis_ResponseMultiBulkStreamHandler()
+            );
+        }
+        if ($this->_options->throw_on_error === false) {
+            $this->_responseReader->setHandler(
+                Predis_Protocol::PREFIX_ERROR, 
+                new Predis_ResponseErrorSilentHandler()
+            );
+        }
+    }
+
+    private function setupConnection($parameters) {
+        if ($parameters !== null && !(is_array($parameters) || is_string($parameters))) {
+            throw new Predis_ClientException('Invalid parameters type (array or string expected)');
+        }
+
+        if (is_array($parameters) && isset($parameters[0])) {
+            $cluster = new Predis_ConnectionCluster($this->_options->key_distribution);
+            foreach ($parameters as $shardParams) {
+                $cluster->add($this->createConnection($shardParams));
+            }
+            $this->setConnection($cluster);
+        }
+        else {
+            $this->setConnection($this->createConnection($parameters));
+        }
+    }
+
+    private function createConnection($parameters) {
+        $params     = $parameters instanceof Predis_ConnectionParameters
+                          ? $parameters
+                          : new Predis_ConnectionParameters($parameters);
+        $connection = new Predis_Connection($params, $this->_responseReader);
+
+        if ($params->password !== null) {
+            $connection->pushInitCommand($this->createCommand(
+                'auth', array($params->password)
+            ));
+        }
+        if ($params->database !== null) {
+            $connection->pushInitCommand($this->createCommand(
+                'select', array($params->database)
+            ));
+        }
+
+        return $connection;
+    }
+
+    private function setConnection(Predis_IConnection $connection) {
+        $this->_connection = $connection;
+    }
+
+    public function setProfile($serverProfile) {
+        if (!($serverProfile instanceof Predis_RedisServerProfile || is_string($serverProfile))) {
+            throw new InvalidArgumentException(
+                "Invalid type for server profile, Predis_RedisServerProfile or string expected"
+            );
+        }
+        $this->_serverProfile = (is_string($serverProfile) 
+            ? Predis_RedisServerProfile::get($serverProfile)
+            : $serverProfile
+        );
+    }
+
+    public function getProfile() {
+        return $this->_serverProfile;
+    }
+
+    public function getResponseReader() {
+        return $this->_responseReader;
+    }
+
+    public function getClientFor($connectionAlias) {
+        if (!Predis_Shared_Utils::isCluster($this->_connection)) {
+            throw new Predis_ClientException(
+                'This method is supported only when the client is connected to a cluster of connections'
+            );
+        }
+
+        $connection = $this->_connection->getConnectionById($connectionAlias);
+        if ($connection === null) {
+            throw new InvalidArgumentException(
+                "Invalid connection alias: '$connectionAlias'"
+            );
+        }
+
+        $newClient = new Predis_Client();
+        $newClient->setupClient($this->_options);
+        $newClient->setConnection($connection);
+        return $newClient;
+    }
+
+    public function connect() {
+        $this->_connection->connect();
+    }
+
+    public function disconnect() {
+        $this->_connection->disconnect();
+    }
+
+    public function isConnected() {
+        return $this->_connection->isConnected();
+    }
+
+    public function getConnection($id = null) {
+        if (!isset($id)) {
+            return $this->_connection;
+        }
+        else {
+            return Predis_Shared_Utils::isCluster($this->_connection) 
+                ? $this->_connection->getConnectionById($id)
+                : $this->_connection;
+        }
+    }
+
+    public function __call($method, $arguments) {
+        $command = $this->_serverProfile->createCommand($method, $arguments);
+        return $this->_connection->executeCommand($command);
+    }
+
+    public function createCommand($method, $arguments = array()) {
+        return $this->_serverProfile->createCommand($method, $arguments);
+    }
+
+    public function executeCommand(Predis_Command $command) {
+        return $this->_connection->executeCommand($command);
+    }
+
+    public function executeCommandOnShards(Predis_Command $command) {
+        $replies = array();
+        if (Predis_Shared_Utils::isCluster($this->_connection)) {
+            foreach($this->_connection as $connection) {
+                $replies[] = $connection->executeCommand($command);
+            }
+        }
+        else {
+            $replies[] = $this->_connection->executeCommand($command);
+        }
+        return $replies;
+    }
+
+    public function rawCommand($rawCommandData, $closesConnection = false) {
+        if (Predis_Shared_Utils::isCluster($this->_connection)) {
+            throw new Predis_ClientException('Cannot send raw commands when connected to a cluster of Redis servers');
+        }
+        return $this->_connection->rawCommand($rawCommandData, $closesConnection);
+    }
+
+    private function sharedInitializer($argv, $initializer) {
+        $argc = count($argv);
+        if ($argc === 0) {
+            return $this->$initializer();
+        }
+        else if ($argc === 1) {
+            list($arg0) = $argv;
+            return is_array($arg0) ? $this->$initializer($arg0) : $this->$initializer(null, $arg0);
+        }
+        else if ($argc === 2) {
+            list($arg0, $arg1) = $argv;
+            return $this->$initializer($arg0, $arg1);
+        }
+        return $this->$initializer($this, $arguments);
+    }
+
+    public function pipeline(/* arguments */) {
+        $args = func_get_args();
+        return $this->sharedInitializer($args, 'initPipeline');
+    }
+
+    public function pipelineSafe($pipelineBlock = null) {
+        return $this->initPipeline(array('safe' => true), $pipelineBlock);
+    }
+
+    private function initPipeline(Array $options = null, $pipelineBlock = null) {
+        $pipeline = null;
+        if (isset($options)) {
+            if (isset($options['safe']) && $options['safe'] == true) {
+                $connection = $this->getConnection();
+                $pipeline   = new Predis_CommandPipeline($this, $connection instanceof Predis_Connection
+                    ? new Predis_Pipeline_SafeExecutor($connection)
+                    : new Predis_Pipeline_SafeClusterExecutor($connection)
+                );
+            }
+            else {
+                $pipeline = new Predis_CommandPipeline($this);
+            }
+        }
+        else {
+            $pipeline = new Predis_CommandPipeline($this);
+        }
+        return $this->pipelineExecute($pipeline, $pipelineBlock);
+    }
+
+    private function pipelineExecute(Predis_CommandPipeline $pipeline, $block) {
+        return $block !== null ? $pipeline->execute($block) : $pipeline;
+    }
+
+    public function multiExec(/* arguments */) {
+        $args = func_get_args();
+        return $this->sharedInitializer($args, 'initMultiExec');
+    }
+
+    private function initMultiExec(Array $options = null, $transBlock = null) {
+        $multi = isset($options) ? new Predis_MultiExecBlock($this, $options) : new Predis_MultiExecBlock($this);
+        return $transBlock !== null ? $multi->execute($transBlock) : $multi;
+    }
+
+    public function pubSubContext() {
+        return new Predis_PubSubContext($this);
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
+interface Predis_IClientOptionsHandler {
+    public function validate($option, $value);
+    public function getDefault();
+}
+
+class Predis_ClientOptionsProfile implements Predis_IClientOptionsHandler {
+    public function validate($option, $value) {
+        if ($value instanceof Predis_RedisServerProfile) {
+            return $value;
+        }
+        if (is_string($value)) {
+            return Predis_RedisServerProfile::get($value);
+        }
+        throw new InvalidArgumentException("Invalid value for option $option");
+    }
+
+    public function getDefault() {
+        return Predis_RedisServerProfile::getDefault();
+    }
+}
+
+class Predis_ClientOptionsKeyDistribution implements Predis_IClientOptionsHandler {
+    public function validate($option, $value) {
+        if ($value instanceof Predis_Distribution_IDistributionStrategy) {
+            return $value;
+        }
+        if (is_string($value)) {
+            $valueReflection = new ReflectionClass($value);
+            if ($valueReflection->isSubclassOf('Predis_Distribution_IDistributionStrategy')) {
+                return new $value;
+            }
+        }
+        throw new InvalidArgumentException("Invalid value for option $option");
+    }
+
+    public function getDefault() {
+        return new Predis_Distribution_HashRing();
+    }
+}
+
+class Predis_ClientOptionsIterableMultiBulk implements Predis_IClientOptionsHandler {
+    public function validate($option, $value) {
+        return (bool) $value;
+    }
+
+    public function getDefault() {
+        return false;
+    }
+}
+
+class Predis_ClientOptionsThrowOnError implements Predis_IClientOptionsHandler {
+    public function validate($option, $value) {
+        return (bool) $value;
+    }
+
+    public function getDefault() {
+        return true;
+    }
+}
+
+class Predis_ClientOptions {
+    private static $_optionsHandlers;
+    private $_options;
+
+    public function __construct($options = null) {
+        self::initializeOptionsHandlers();
+        $this->initializeOptions($options !== null ? $options : array());
+    }
+
+    private static function initializeOptionsHandlers() {
+        if (!isset(self::$_optionsHandlers)) {
+            self::$_optionsHandlers = self::getOptionsHandlers();
+        }
+    }
+
+    private static function getOptionsHandlers() {
+        return array(
+            'profile'    => new Predis_ClientOptionsProfile(),
+            'key_distribution' => new Predis_ClientOptionsKeyDistribution(),
+            'iterable_multibulk' => new Predis_ClientOptionsIterableMultiBulk(),
+            'throw_on_error' => new Predis_ClientOptionsThrowOnError(),
+        );
+    }
+
+    private function initializeOptions($options) {
+        foreach ($options as $option => $value) {
+            if (isset(self::$_optionsHandlers[$option])) {
+                $handler = self::$_optionsHandlers[$option];
+                $this->_options[$option] = $handler->validate($option, $value);
+            }
+        }
+    }
+
+    public function __get($option) {
+        if (!isset($this->_options[$option])) {
+            $defaultValue = self::$_optionsHandlers[$option]->getDefault();
+            $this->_options[$option] = $defaultValue;
+        }
+        return $this->_options[$option];
+    }
+
+    public function __isset($option) {
+        return isset(self::$_optionsHandlers[$option]);
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
+class Predis_Protocol {
+    const NEWLINE = "\r\n";
+    const OK      = 'OK';
+    const ERROR   = 'ERR';
+    const QUEUED  = 'QUEUED';
+    const NULL    = 'nil';
+
+    const PREFIX_STATUS     = '+';
+    const PREFIX_ERROR      = '-';
+    const PREFIX_INTEGER    = ':';
+    const PREFIX_BULK       = '$';
+    const PREFIX_MULTI_BULK = '*';
+}
+
+abstract class Predis_Command {
+    private $_arguments, $_hash;
+
+    public abstract function getCommandId();
+
+    public abstract function serializeRequest($command, $arguments);
+
+    public function canBeHashed() {
+        return true;
+    }
+
+    public function getHash(Predis_Distribution_IDistributionStrategy $distributor) {
+        if (isset($this->_hash)) {
+            return $this->_hash;
+        }
+        else {
+            if (isset($this->_arguments[0])) {
+                // TODO: should we throw an exception if the command does 
+                //       not support sharding?
+                $key = $this->_arguments[0];
+
+                $start = strpos($key, '{');
+                $end   = strpos($key, '}');
+                if ($start !== false && $end !== false) {
+                    $key = substr($key, ++$start, $end - $start);
+                }
+
+                $this->_hash = $distributor->generateKey($key);
+                return $this->_hash;
+            }
+        }
+        return null;
+    }
+
+    public function closesConnection() {
+        return false;
+    }
+
+    protected function filterArguments(Array $arguments) {
+        return $arguments;
+    }
+
+    public function setArguments(/* arguments */) {
+        $this->_arguments = $this->filterArguments(func_get_args());
+        unset($this->_hash);
+    }
+
+    public function setArgumentsArray(Array $arguments) {
+        $this->_arguments = $this->filterArguments($arguments);
+        unset($this->_hash);
+    }
+
+    public function getArguments() {
+        return isset($this->_arguments) ? $this->_arguments : array();
+    }
+
+    public function getArgument($index = 0) {
+        return isset($this->_arguments[$index]) ? $this->_arguments[$index] : null;
+    }
+
+    public function parseResponse($data) {
+        return $data;
+    }
+
+    public final function invoke() {
+        return $this->serializeRequest($this->getCommandId(), $this->getArguments());
+    }
+}
+
+abstract class Predis_InlineCommand extends Predis_Command {
+    public function serializeRequest($command, $arguments) {
+        if (isset($arguments[0]) && is_array($arguments[0])) {
+            $arguments[0] = implode($arguments[0], ' ');
+        }
+        return $command . (count($arguments) > 0
+            ? ' ' . implode($arguments, ' ') . Predis_Protocol::NEWLINE 
+            : Predis_Protocol::NEWLINE
+        );
+    }
+}
+
+abstract class Predis_BulkCommand extends Predis_Command {
+    public function serializeRequest($command, $arguments) {
+        $data = array_pop($arguments);
+        if (is_array($data)) {
+            $data = implode($data, ' ');
+        }
+        return $command . ' ' . implode($arguments, ' ') . ' ' . strlen($data) . 
+            Predis_Protocol::NEWLINE . $data . Predis_Protocol::NEWLINE;
+    }
+}
+
+abstract class Predis_MultiBulkCommand extends Predis_Command {
+    public function serializeRequest($command, $arguments) {
+        $cmd_args = null;
+        $argsc    = count($arguments);
+
+        if ($argsc === 1 && is_array($arguments[0])) {
+            $cmd_args = $arguments[0];
+            $argsc = count($cmd_args);
+        }
+        else {
+            $cmd_args = $arguments;
+        }
+
+        $newline = Predis_Protocol::NEWLINE;
+        $cmdlen  = strlen($command);
+        $reqlen  = $argsc + 1;
+
+        $buffer = "*{$reqlen}{$newline}\${$cmdlen}{$newline}{$command}{$newline}";
+        foreach ($cmd_args as $argument) {
+            $arglen  = strlen($argument);
+            $buffer .= "\${$arglen}{$newline}{$argument}{$newline}";
+        }
+
+        return $buffer;
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
+interface Predis_IResponseHandler {
+    function handle(Predis_Connection $connection, $payload);
+}
+
+class Predis_ResponseStatusHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $status) {
+        if ($status === Predis_Protocol::OK) {
+            return true;
+        }
+        else if ($status === Predis_Protocol::QUEUED) {
+            return new Predis_ResponseQueued();
+        }
+        return $status;
+    }
+}
+
+class Predis_ResponseErrorHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $errorMessage) {
+        throw new Predis_ServerException(substr($errorMessage, 4));
+    }
+}
+
+class Predis_ResponseErrorSilentHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $errorMessage) {
+        return new Predis_ResponseError(substr($errorMessage, 4));
+    }
+}
+
+class Predis_ResponseBulkHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $dataLength) {
+        if (!is_numeric($dataLength)) {
+            Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                $connection, "Cannot parse '$dataLength' as data length"
+            ));
+        }
+
+        if ($dataLength > 0) {
+            $value = $connection->readBytes($dataLength);
+            self::discardNewLine($connection);
+            return $value;
+        }
+        else if ($dataLength == 0) {
+            self::discardNewLine($connection);
+            return '';
+        }
+
+        return null;
+    }
+
+    private static function discardNewLine(Predis_Connection $connection) {
+        if ($connection->readBytes(2) !== Predis_Protocol::NEWLINE) {
+            Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                $connection, 'Did not receive a new-line at the end of a bulk response'
+            ));
+        }
+    }
+}
+
+class Predis_ResponseMultiBulkHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $rawLength) {
+        if (!is_numeric($rawLength)) {
+            Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                $connection, "Cannot parse '$rawLength' as data length"
+            ));
+        }
+
+        $listLength = (int) $rawLength;
+        if ($listLength === -1) {
+            return null;
+        }
+
+        $list = array();
+
+        if ($listLength > 0) {
+            $reader = $connection->getResponseReader();
+            for ($i = 0; $i < $listLength; $i++) {
+                $list[] = $reader->read($connection);
+            }
+        }
+
+        return $list;
+    }
+}
+
+class Predis_ResponseMultiBulkStreamHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $rawLength) {
+        if (!is_numeric($rawLength)) {
+            Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                $connection, "Cannot parse '$rawLength' as data length"
+            ));
+        }
+        return new Predis_Shared_MultiBulkResponseIterator($connection, (int)$rawLength);
+    }
+}
+
+class Predis_ResponseIntegerHandler implements Predis_IResponseHandler {
+    public function handle(Predis_Connection $connection, $number) {
+        if (is_numeric($number)) {
+            return (int) $number;
+        }
+        else {
+            if ($number !== Predis_Protocol::NULL) {
+                Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                    $connection, "Cannot parse '$number' as numeric response"
+                ));
+            }
+            return null;
+        }
+    }
+}
+
+class Predis_ResponseReader {
+    private $_prefixHandlers;
+
+    public function __construct() {
+        $this->initializePrefixHandlers();
+    }
+
+    private function initializePrefixHandlers() {
+        $this->_prefixHandlers = array(
+            Predis_Protocol::PREFIX_STATUS     => new Predis_ResponseStatusHandler(), 
+            Predis_Protocol::PREFIX_ERROR      => new Predis_ResponseErrorHandler(), 
+            Predis_Protocol::PREFIX_INTEGER    => new Predis_ResponseIntegerHandler(), 
+            Predis_Protocol::PREFIX_BULK       => new Predis_ResponseBulkHandler(), 
+            Predis_Protocol::PREFIX_MULTI_BULK => new Predis_ResponseMultiBulkHandler(), 
+        );
+    }
+
+    public function setHandler($prefix, Predis_IResponseHandler $handler) {
+        $this->_prefixHandlers[$prefix] = $handler;
+    }
+
+    public function getHandler($prefix) {
+        if (isset($this->_prefixHandlers[$prefix])) {
+            return $this->_prefixHandlers[$prefix];
+        }
+    }
+
+    public function read(Predis_Connection $connection) {
+        $header = $connection->readLine();
+        if ($header === '') {
+            Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                $connection, 'Unexpected empty header'
+            ));
+        }
+
+        $prefix  = $header[0];
+        $payload = strlen($header) > 1 ? substr($header, 1) : '';
+
+        if (!isset($this->_prefixHandlers[$prefix])) {
+            Predis_Shared_Utils::onCommunicationException(new Predis_MalformedServerResponse(
+                $connection, "Unknown prefix '$prefix'"
+            ));
+        }
+
+        $handler = $this->_prefixHandlers[$prefix];
+        return $handler->handle($connection, $payload);
+    }
+}
+
+class Predis_ResponseError {
+    private $_message;
+
+    public function __construct($message) {
+        $this->_message = $message;
+    }
+
+    public function __get($property) {
+        if ($property == 'error') {
+            return true;
+        }
+        if ($property == 'message') {
+            return $this->_message;
+        }
+    }
+
+    public function __isset($property) {
+        return $property === 'error';
+    }
+
+    public function __toString() {
+        return $this->_message;
+    }
+}
+
+class Predis_ResponseQueued {
+    public $queued = true;
+
+    public function __toString() {
+        return Predis_Protocol::QUEUED;
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
+class Predis_CommandPipeline {
+    private $_redisClient, $_pipelineBuffer, $_returnValues, $_running, $_executor;
+
+    public function __construct(Predis_Client $redisClient, Predis_Pipeline_IPipelineExecutor $executor = null) {
+        $this->_redisClient    = $redisClient;
+        $this->_executor       = $executor !== null ? $executor : new Predis_Pipeline_StandardExecutor();
+        $this->_pipelineBuffer = array();
+        $this->_returnValues   = array();
+    }
+
+    public function __call($method, $arguments) {
+        $command = $this->_redisClient->createCommand($method, $arguments);
+        $this->recordCommand($command);
+        return $this;
+    }
+
+    private function recordCommand(Predis_Command $command) {
+        $this->_pipelineBuffer[] = $command;
+    }
+
+    private function getRecordedCommands() {
+        return $this->_pipelineBuffer;
+    }
+
+    public function flushPipeline() {
+        if (count($this->_pipelineBuffer) > 0) {
+            $connection = $this->_redisClient->getConnection();
+            $this->_returnValues = array_merge(
+                $this->_returnValues, 
+                $this->_executor->execute($connection, $this->_pipelineBuffer)
+            );
+            $this->_pipelineBuffer = array();
+        }
+        return $this;
+    }
+
+    private function setRunning($bool) {
+        if ($bool == true && $this->_running == true) {
+            throw new Predis_ClientException("This pipeline is already opened");
+        }
+        $this->_running = $bool;
+    }
+
+    public function execute($block = null) {
+        if ($block && !is_callable($block)) {
+            throw new InvalidArgumentException('Argument passed must be a callable object');
+        }
+
+        // TODO: do not reuse previously executed pipelines
+        $this->setRunning(true);
+        $pipelineBlockException = null;
+
+        try {
+            if ($block !== null) {
+                $block($this);
+            }
+            $this->flushPipeline();
+        }
+        catch (Exception $exception) {
+            $pipelineBlockException = $exception;
+        }
+
+        $this->setRunning(false);
+
+        if ($pipelineBlockException !== null) {
+            throw $pipelineBlockException;
+        }
+
+        return $this->_returnValues;
+    }
+}
+
+class Predis_MultiExecBlock {
+    private $_initialized, $_discarded, $_insideBlock, $_checkAndSet;
+    private $_redisClient, $_options, $_commands;
+    private $_supportsWatch;
+
+    public function __construct(Predis_Client $redisClient, Array $options = null) {
+        $this->checkCapabilities($redisClient);
+        $this->_options = isset($options) ? $options : array();
+        $this->_redisClient = $redisClient;
+        $this->reset();
+    }
+
+    private function checkCapabilities(Predis_Client $redisClient) {
+        if (Predis_Shared_Utils::isCluster($redisClient->getConnection())) {
+            throw new Predis_ClientException(
+                'Cannot initialize a MULTI/EXEC context over a cluster of connections'
+            );
+        }
+        $profile = $redisClient->getProfile();
+        if ($profile->supportsCommands(array('multi', 'exec', 'discard')) === false) {
+            throw new Predis_ClientException(
+                'The current profile does not support MULTI, EXEC and DISCARD commands'
+            );
+        }
+        $this->_supportsWatch = $profile->supportsCommands(array('watch', 'unwatch'));
+    }
+
+    private function isWatchSupported() {
+        if ($this->_supportsWatch === false) {
+            throw new Predis_ClientException(
+                'The current profile does not support WATCH and UNWATCH commands'
+            );
+        }
+    }
+
+    private function reset() {
+        $this->_initialized = false;
+        $this->_discarded   = false;
+        $this->_checkAndSet = false;
+        $this->_insideBlock = false;
+        $this->_commands    = array();
+    }
+
+    private function initialize() {
+        if ($this->_initialized === true) {
+            return;
+        }
+        $options = &$this->_options;
+        $this->_checkAndSet = isset($options['cas']) && $options['cas'];
+        if (isset($options['watch'])) {
+            $this->watch($options['watch']);
+        }
+        if (!$this->_checkAndSet || ($this->_discarded && $this->_checkAndSet)) {
+            $this->_redisClient->multi();
+            if ($this->_discarded) {
+                $this->_checkAndSet = false;
+            }
+        }
+        $this->_initialized = true;
+        $this->_discarded   = false;
+    }
+
+    public function __call($method, $arguments) {
+        $this->initialize();
+        $client = $this->_redisClient;
+        if ($this->_checkAndSet) {
+            return call_user_func_array(array($client, $method), $arguments);
+        }
+        $command  = $client->createCommand($method, $arguments);
+        $response = $client->executeCommand($command);
+        if (!isset($response->queued)) {
+            $this->malformedServerResponse(
+                'The server did not respond with a QUEUED status reply'
+            );
+        }
+        $this->_commands[] = $command;
+        return $this;
+    }
+
+    public function watch($keys) {
+        $this->isWatchSupported();
+        if ($this->_initialized && !$this->_checkAndSet) {
+            throw new Predis_ClientException('WATCH inside MULTI is not allowed');
+        }
+        return $this->_redisClient->watch($keys);
+    }
+
+    public function multi() {
+        if ($this->_initialized && $this->_checkAndSet) {
+            $this->_checkAndSet = false;
+            $this->_redisClient->multi();
+            return $this;
+        }
+        $this->initialize();
+        return $this;
+    }
+
+    public function unwatch() {
+        $this->isWatchSupported();
+        $this->_redisClient->unwatch();
+        return $this;
+    }
+
+    public function discard() {
+        $this->_redisClient->discard();
+        $this->reset();
+        $this->_discarded = true;
+        return $this;
+    }
+
+    public function exec() {
+        return $this->execute();
+    }
+
+    private function checkBeforeExecution($block) {
+        if ($this->_insideBlock === true) {
+            throw new Predis_ClientException(
+                "Cannot invoke 'execute' or 'exec' inside an active client transaction block"
+            );
+        }
+        if ($block) {
+            if (!is_callable($block)) {
+                throw new InvalidArgumentException(
+                    'Argument passed must be a callable object'
+                );
+            }
+            if (count($this->_commands) > 0) {
+                throw new Predis_ClientException(
+                    'Cannot execute a transaction block after using fluent interface'
+                );
+            }
+        }
+        if (isset($this->_options['retry']) && !isset($block)) {
+            $this->discard();
+            throw new InvalidArgumentException(
+                'Automatic retries can be used only when a transaction block is provided'
+            );
+        }
+    }
+
+    public function execute($block = null) {
+        $this->checkBeforeExecution($block);
+
+        $reply = null;
+        $returnValues = array();
+        $attemptsLeft = isset($this->_options['retry']) ? (int)$this->_options['retry'] : 0;
+        do {
+            $blockException = null;
+            if ($block !== null) {
+                $this->_insideBlock = true;
+                try {
+                    $block($this);
+                }
+                catch (Predis_CommunicationException $exception) {
+                    $blockException = $exception;
+                }
+                catch (Predis_ServerException $exception) {
+                    $blockException = $exception;
+                }
+                catch (Exception $exception) {
+                    $blockException = $exception;
+                    if ($this->_initialized === true) {
+                        $this->discard();
+                    }
+                }
+                $this->_insideBlock = false;
+                if ($blockException !== null) {
+                    throw $blockException;
+                }
+            }
+
+            if ($this->_initialized === false || count($this->_commands) == 0) {
+                return;
+            }
+
+            $reply = $this->_redisClient->exec();
+            if ($reply === null) {
+                if ($attemptsLeft === 0) {
+                    throw new Predis_AbortedMultiExec(
+                        'The current transaction has been aborted by the server'
+                    );
+                }
+                $this->reset();
+                continue;
+            }
+            break;
+        } while ($attemptsLeft-- > 0);
+
+
+        $execReply = $reply instanceof Iterator ? iterator_to_array($reply) : $reply;
+        $sizeofReplies = count($execReply);
+
+        $commands = &$this->_commands;
+        if ($sizeofReplies !== count($commands)) {
+            $this->malformedServerResponse(
+                'Unexpected number of responses for a MultiExecBlock'
+            );
+        }
+        for ($i = 0; $i < $sizeofReplies; $i++) {
+            $returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof Iterator
+                ? iterator_to_array($execReply[$i])
+                : $execReply[$i]
+            );
+            unset($commands[$i]);
+        }
+
+        return $returnValues;
+    }
+
+    private function malformedServerResponse($message) {
+        // Since a MULTI/EXEC block cannot be initialized over a clustered 
+        // connection, we can safely assume that Predis_Client::getConnection() 
+        // will always return an instance of Predis_Connection.
+        Predis_Shared_Utils::onCommunicationException(
+            new Predis_MalformedServerResponse(
+                $this->_redisClient->getConnection(), $message
+            )
+        );
+    }
+}
+
+class Predis_PubSubContext implements Iterator {
+    const SUBSCRIBE    = 'subscribe';
+    const UNSUBSCRIBE  = 'unsubscribe';
+    const PSUBSCRIBE   = 'psubscribe';
+    const PUNSUBSCRIBE = 'punsubscribe';
+    const MESSAGE      = 'message';
+    const PMESSAGE     = 'pmessage';
+
+    const STATUS_VALID       = 0x0001;
+    const STATUS_SUBSCRIBED  = 0x0010;
+    const STATUS_PSUBSCRIBED = 0x0100;
+
+    private $_redisClient, $_position;
+
+    public function __construct(Predis_Client $redisClient) {
+        $this->checkCapabilities($redisClient);
+        $this->_redisClient = $redisClient;
+        $this->_statusFlags = self::STATUS_VALID;
+    }
+
+    public function __destruct() {
+        if ($this->valid()) {
+            $this->closeContext();
+        }
+    }
+
+    private function checkCapabilities(Predis_Client $redisClient) {
+        if (Predis_Shared_Utils::isCluster($redisClient->getConnection())) {
+            throw new Predis_ClientException(
+                'Cannot initialize a PUB/SUB context over a cluster of connections'
+            );
+        }
+        $profile = $redisClient->getProfile();
+        $commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
+        if ($profile->supportsCommands($commands) === false) {
+            throw new Predis_ClientException(
+                'The current profile does not support PUB/SUB related commands'
+            );
+        }
+    }
+
+    private function isFlagSet($value) {
+        return ($this->_statusFlags & $value) === $value;
+    }
+
+    public function subscribe(/* arguments */) {
+        $args = func_get_args();
+        $this->writeCommand(self::SUBSCRIBE, $args);
+        $this->_statusFlags |= self::STATUS_SUBSCRIBED;
+    }
+
+    public function unsubscribe(/* arguments */) {
+        $args = func_get_args();
+        $this->writeCommand(self::UNSUBSCRIBE, $args);
+    }
+
+    public function psubscribe(/* arguments */) {
+        $args = func_get_args();
+        $this->writeCommand(self::PSUBSCRIBE, $args);
+        $this->_statusFlags |= self::STATUS_PSUBSCRIBED;
+    }
+
+    public function punsubscribe(/* arguments */) {
+        $args = func_get_args();
+        $this->writeCommand(self::PUNSUBSCRIBE, $args);
+    }
+
+    public function closeContext() {
+        if ($this->valid()) {
+            if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
+                $this->unsubscribe();
+            }
+            if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
+                $this->punsubscribe();
+            }
+        }
+    }
+
+    private function writeCommand($method, $arguments) {
+        if (count($arguments) === 1 && is_array($arguments[0])) {
+            $arguments = $arguments[0];
+        }
+        $command = $this->_redisClient->createCommand($method, $arguments);
+        $this->_redisClient->getConnection()->writeCommand($command);
+    }
+
+    public function rewind() {
+        // NOOP
+    }
+
+    public function current() {
+        return $this->getValue();
+    }
+
+    public function key() {
+        return $this->_position;
+    }
+
+    public function next() {
+        if ($this->isFlagSet(self::STATUS_VALID)) {
+            $this->_position++;
+        }
+        return $this->_position;
+    }
+
+    public function valid() {
+        $subscriptions = self::STATUS_SUBSCRIBED + self::STATUS_PSUBSCRIBED;
+        return $this->isFlagSet(self::STATUS_VALID)
+            && ($this->_statusFlags & $subscriptions) > 0;
+    }
+
+    private function invalidate() {
+        $this->_statusFlags = 0x0000;
+    }
+
+    private function getValue() {
+        $reader     = $this->_redisClient->getResponseReader();
+        $connection = $this->_redisClient->getConnection();
+        $response   = $reader->read($connection);
+
+        switch ($response[0]) {
+            case self::SUBSCRIBE:
+            case self::UNSUBSCRIBE:
+            case self::PSUBSCRIBE:
+            case self::PUNSUBSCRIBE:
+                if ($response[2] === 0) {
+                    $this->invalidate();
+                }
+            case self::MESSAGE:
+                return (object) array(
+                    'kind'    => $response[0],
+                    'channel' => $response[1],
+                    'payload' => $response[2],
+                );
+            case self::PMESSAGE:
+                return (object) array(
+                    'kind'    => $response[0],
+                    'pattern' => $response[1],
+                    'channel' => $response[2],
+                    'payload' => $response[3],
+                );
+            default:
+                throw new Predis_ClientException(
+                    "Received an unknown message type {$response[0]} inside of a pubsub context"
+                );
+        }
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
+class Predis_ConnectionParameters {
+    const DEFAULT_HOST = '127.0.0.1';
+    const DEFAULT_PORT = 6379;
+    const DEFAULT_TIMEOUT = 5;
+    private $_parameters;
+
+    public function __construct($parameters = null) {
+        $parameters = $parameters !== null ? $parameters : array();
+        $this->_parameters = is_array($parameters) 
+            ? self::filterConnectionParams($parameters) 
+            : self::parseURI($parameters);
+    }
+
+    private static function parseURI($uri) {
+        $parsed = @parse_url($uri);
+
+        if ($parsed == false || $parsed['scheme'] != 'redis' || $parsed['host'] == null) {
+            throw new Predis_ClientException("Invalid URI: $uri");
+        }
+
+        if (array_key_exists('query', $parsed)) {
+            $details = array();
+            foreach (explode('&', $parsed['query']) as $kv) {
+                list($k, $v) = explode('=', $kv);
+                switch ($k) {
+                    case 'database':
+                        $details['database'] = $v;
+                        break;
+                    case 'password':
+                        $details['password'] = $v;
+                        break;
+                    case 'connection_async':
+                        $details['connection_async'] = $v;
+                        break;
+                    case 'connection_persistent':
+                        $details['connection_persistent'] = $v;
+                        break;
+                    case 'connection_timeout':
+                        $details['connection_timeout'] = $v;
+                        break;
+                    case 'read_write_timeout':
+                        $details['read_write_timeout'] = $v;
+                        break;
+                    case 'alias':
+                        $details['alias'] = $v;
+                        break;
+                    case 'weight':
+                        $details['weight'] = $v;
+                        break;
+                }
+            }
+            $parsed = array_merge($parsed, $details);
+        }
+
+        return self::filterConnectionParams($parsed);
+    }
+
+    private static function getParamOrDefault(Array $parameters, $param, $default = null) {
+        return array_key_exists($param, $parameters) ? $parameters[$param] : $default;
+    }
+
+    private static function filterConnectionParams($parameters) {
+        return array(
+            'host' => self::getParamOrDefault($parameters, 'host', self::DEFAULT_HOST), 
+            'port' => (int) self::getParamOrDefault($parameters, 'port', self::DEFAULT_PORT), 
+            'database' => self::getParamOrDefault($parameters, 'database'), 
+            'password' => self::getParamOrDefault($parameters, 'password'), 
+            'connection_async'   => self::getParamOrDefault($parameters, 'connection_async', false), 
+            'connection_persistent' => self::getParamOrDefault($parameters, 'connection_persistent', false), 
+            'connection_timeout' => self::getParamOrDefault($parameters, 'connection_timeout', self::DEFAULT_TIMEOUT), 
+            'read_write_timeout' => self::getParamOrDefault($parameters, 'read_write_timeout'), 
+            'alias'  => self::getParamOrDefault($parameters, 'alias'), 
+            'weight' => self::getParamOrDefault($parameters, 'weight'), 
+        );
+    }
+
+    public function __get($parameter) {
+        return $this->_parameters[$parameter];
+    }
+
+    public function __isset($parameter) {
+        return isset($this->_parameters[$parameter]);
+    }
+}
+
+interface Predis_IConnection {
+    public function connect();
+    public function disconnect();
+    public function isConnected();
+    public function writeCommand(Predis_Command $command);
+    public function readResponse(Predis_Command $command);
+    public function executeCommand(Predis_Command $command);
+}
+
+class Predis_Connection implements Predis_IConnection {
+    private $_params, $_socket, $_initCmds, $_reader;
+
+    public function __construct(Predis_ConnectionParameters $parameters, Predis_ResponseReader $reader = null) {
+        $this->_params   = $parameters;
+        $this->_initCmds = array();
+        $this->_reader   = $reader !== null ? $reader : new Predis_ResponseReader();
+    }
+
+    public function __destruct() {
+        if (!$this->_params->connection_persistent) {
+            $this->disconnect();
+        }
+    }
+
+    public function isConnected() {
+        return is_resource($this->_socket);
+    }
+
+    public function connect() {
+        if ($this->isConnected()) {
+            throw new Predis_ClientException('Connection already estabilished');
+        }
+        $uri = sprintf('tcp://%s:%d/', $this->_params->host, $this->_params->port);
+        $connectFlags = STREAM_CLIENT_CONNECT;
+        if ($this->_params->connection_async) {
+            $connectFlags |= STREAM_CLIENT_ASYNC_CONNECT;
+        }
+        if ($this->_params->connection_persistent) {
+            $connectFlags |= STREAM_CLIENT_PERSISTENT;
+        }
+        $this->_socket = @stream_socket_client(
+            $uri, $errno, $errstr, $this->_params->connection_timeout, $connectFlags
+        );
+
+        if (!$this->_socket) {
+            $this->onCommunicationException(trim($errstr), $errno);
+        }
+
+        if (isset($this->_params->read_write_timeout)) {
+            $timeoutSeconds  = floor($this->_params->read_write_timeout);
+            $timeoutUSeconds = ($this->_params->read_write_timeout - $timeoutSeconds) * 1000000;
+            stream_set_timeout($this->_socket, $timeoutSeconds, $timeoutUSeconds);
+        }
+
+        if (count($this->_initCmds) > 0){
+            $this->sendInitializationCommands();
+        }
+    }
+
+    public function disconnect() {
+        if ($this->isConnected()) {
+            fclose($this->_socket);
+        }
+    }
+
+    public function pushInitCommand(Predis_Command $command){
+        $this->_initCmds[] = $command;
+    }
+
+    private function sendInitializationCommands() {
+        foreach ($this->_initCmds as $command) {
+            $this->writeCommand($command);
+        }
+        foreach ($this->_initCmds as $command) {
+            $this->readResponse($command);
+        }
+    }
+
+    private function onCommunicationException($message, $code = null) {
+        Predis_Shared_Utils::onCommunicationException(
+            new Predis_CommunicationException($this, $message, $code)
+        );
+    }
+
+    public function writeCommand(Predis_Command $command) {
+        $this->writeBytes($command->invoke());
+    }
+
+    public function readResponse(Predis_Command $command) {
+        $response = $this->_reader->read($this);
+        $skipparse = isset($response->queued) || isset($response->error);
+        return $skipparse ? $response : $command->parseResponse($response);
+    }
+
+    public function executeCommand(Predis_Command $command) {
+        $this->writeCommand($command);
+        if ($command->closesConnection()) {
+            return $this->disconnect();
+        }
+        return $this->readResponse($command);
+    }
+
+    public function rawCommand($rawCommandData, $closesConnection = false) {
+        $this->writeBytes($rawCommandData);
+        if ($closesConnection) {
+            $this->disconnect();
+            return;
+        }
+        return $this->_reader->read($this);
+    }
+
+    public function writeBytes($value) {
+        $socket = $this->getSocket();
+        while (($length = strlen($value)) > 0) {
+            $written = fwrite($socket, $value);
+            if ($length === $written) {
+                return true;
+            }
+            if ($written === false || $written === 0) {
+                $this->onCommunicationException('Error while writing bytes to the server');
+            }
+            $value = substr($value, $written);
+        }
+        return true;
+    }
+
+    public function readBytes($length) {
+        if ($length == 0) {
+            throw new InvalidArgumentException('Length parameter must be greater than 0');
+        }
+        $socket = $this->getSocket();
+        $value  = '';
+        do {
+            $chunk = fread($socket, $length);
+            if ($chunk === false || $chunk === '') {
+                $this->onCommunicationException('Error while reading bytes from the server');
+            }
+            $value .= $chunk;
+        }
+        while (($length -= strlen($chunk)) > 0);
+        return $value;
+    }
+
+    public function readLine() {
+        $socket = $this->getSocket();
+        $value  = '';
+        do {
+            $chunk = fgets($socket);
+            if ($chunk === false || strlen($chunk) == 0) {
+                $this->onCommunicationException('Error while reading line from the server');
+            }
+            $value .= $chunk;
+        }
+        while (substr($value, -2) !== Predis_Protocol::NEWLINE);
+        return substr($value, 0, -2);
+    }
+
+    public function getSocket() {
+        if (!$this->isConnected()) {
+            $this->connect();
+        }
+        return $this->_socket;
+    }
+
+    public function getResponseReader() {
+        return $this->_reader;
+    }
+
+    public function getParameters() {
+        return $this->_params;
+    }
+
+    public function __toString() {
+        return sprintf('%s:%d', $this->_params->host, $this->_params->port);
+    }
+}
+
+class Predis_ConnectionCluster implements Predis_IConnection, IteratorAggregate {
+    private $_pool, $_distributor;
+
+    public function __construct(Predis_Distribution_IDistributionStrategy $distributor = null) {
+        $this->_pool = array();
+        $this->_distributor = $distributor !== null ? $distributor : new Predis_Distribution_HashRing();
+    }
+
+    public function isConnected() {
+        foreach ($this->_pool as $connection) {
+            if ($connection->isConnected()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public function connect() {
+        foreach ($this->_pool as $connection) {
+            $connection->connect();
+        }
+    }
+
+    public function disconnect() {
+        foreach ($this->_pool as $connection) {
+            $connection->disconnect();
+        }
+    }
+
+    public function add(Predis_Connection $connection) {
+        $parameters = $connection->getParameters();
+        if (isset($parameters->alias)) {
+            $this->_pool[$parameters->alias] = $connection;
+        }
+        else {
+            $this->_pool[] = $connection;
+        }
+        $this->_distributor->add($connection, $parameters->weight);
+    }
+
+    public function getConnection(Predis_Command $command) {
+        if ($command->canBeHashed() === false) {
+            throw new Predis_ClientException(
+                sprintf("Cannot send '%s' commands to a cluster of connections", $command->getCommandId())
+            );
+        }
+        return $this->_distributor->get($command->getHash($this->_distributor));
+    }
+
+    public function getConnectionById($id = null) {
+        $alias = $id !== null ? $id : 0;
+        return isset($this->_pool[$alias]) ? $this->_pool[$alias] : null;
+    }
+
+    public function getIterator() {
+        return new ArrayIterator($this->_pool);
+    }
+
+    public function writeCommand(Predis_Command $command) {
+        $this->getConnection($command)->writeCommand($command);
+    }
+
+    public function readResponse(Predis_Command $command) {
+        return $this->getConnection($command)->readResponse($command);
+    }
+
+    public function executeCommand(Predis_Command $command) {
+        $connection = $this->getConnection($command);
+        $connection->writeCommand($command);
+        return $connection->readResponse($command);
+    }
+}
+
+/* ------------------------------------------------------------------------- */
+
+abstract class Predis_RedisServerProfile {
+    private static $_serverProfiles;
+    private $_registeredCommands;
+
+    public function __construct() {
+        $this->_registeredCommands = $this->getSupportedCommands();
+    }
+
+    public abstract function getVersion();
+
+    protected abstract function getSupportedCommands();
+
+    public static function getDefault() {
+        return self::get('default');
+    }
+
+    public static function getDevelopment() {
+        return self::get('dev');
+    }
+
+    private static function predisServerProfiles() {
+        return array(
+            '1.2'     => 'Predis_RedisServer_v1_2',
+            '2.0'     => 'Predis_RedisServer_v2_0',
+            'default' => 'Predis_RedisServer_v2_0',
+            'dev'     => 'Predis_RedisServer_vNext',
+        );
+    }
+
+    public static function registerProfile($profileClass, $aliases) {
+        if (!isset(self::$_serverProfiles)) {
+            self::$_serverProfiles = self::predisServerProfiles();
+        }
+
+        $profileReflection = new ReflectionClass($profileClass);
+
+        if (!$profileReflection->isSubclassOf('Predis_RedisServerProfile')) {
+            throw new Predis_ClientException("Cannot register '$profileClass' as it is not a valid profile class");
+        }
+
+        if (is_array($aliases)) {
+            foreach ($aliases as $alias) {
+                self::$_serverProfiles[$alias] = $profileClass;
+            }
+        }
+        else {
+            self::$_serverProfiles[$aliases] = $profileClass;
+        }
+    }
+
+    public static function get($version) {
+        if (!isset(self::$_serverProfiles)) {
+            self::$_serverProfiles = self::predisServerProfiles();
+        }
+        if (!isset(self::$_serverProfiles[$version])) {
+            throw new Predis_ClientException("Unknown server profile: $version");
+        }
+        $profile = self::$_serverProfiles[$version];
+        return new $profile();
+    }
+
+    public function supportsCommands(Array $commands) {
+        foreach ($commands as $command) {
+            if ($this->supportsCommand($command) === false) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public function supportsCommand($command) {
+        return isset($this->_registeredCommands[$command]);
+    }
+
+    public function createCommand($method, $arguments = array()) {
+        if (!isset($this->_registeredCommands[$method])) {
+            throw new Predis_ClientException("'$method' is not a registered Redis command");
+        }
+        $commandClass = $this->_registeredCommands[$method];
+        $command = new $commandClass();
+        $command->setArgumentsArray($arguments);
+        return $command;
+    }
+
+    public function registerCommands(Array $commands) {
+        foreach ($commands as $command => $aliases) {
+            $this->registerCommand($command, $aliases);
+        }
+    }
+
+    public function registerCommand($command, $aliases) {
+        $commandReflection = new ReflectionClass($command);
+
+        if (!$commandReflection->isSubclassOf('Predis_Command')) {
+            throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
+        }
+
+        if (is_array($aliases)) {
+            foreach ($aliases as $alias) {
+                $this->_registeredCommands[$alias] = $command;
+            }
+        }
+        else {
+            $this->_registeredCommands[$aliases] = $command;
+        }
+    }
+
+    public function __toString() {
+        return $this->getVersion();
+    }
+}
+
+class Predis_RedisServer_v1_2 extends Predis_RedisServerProfile {
+    public function getVersion() { return '1.2'; }
+    public function getSupportedCommands() {
+        return array(
+            /* miscellaneous commands */
+            'ping'      => 'Predis_Commands_Ping',
+            'echo'      => 'Predis_Commands_DoEcho',
+            'auth'      => 'Predis_Commands_Auth',
+
+            /* connection handling */
+            'quit'      => 'Predis_Commands_Quit',
+
+            /* commands operating on string values */
+            'set'                     => 'Predis_Commands_Set',
+            'setnx'                   => 'Predis_Commands_SetPreserve',
+                'setPreserve'         => 'Predis_Commands_SetPreserve',
+            'mset'                    => 'Predis_Commands_SetMultiple',
+                'setMultiple'         => 'Predis_Commands_SetMultiple',
+            'msetnx'                  => 'Predis_Commands_SetMultiplePreserve',
+                'setMultiplePreserve' => 'Predis_Commands_SetMultiplePreserve',
+            'get'                     => 'Predis_Commands_Get',
+            'mget'                    => 'Predis_Commands_GetMultiple',
+                'getMultiple'         => 'Predis_Commands_GetMultiple',
+            'getset'                  => 'Predis_Commands_GetSet',
+                'getSet'              => 'Predis_Commands_GetSet',
+            'incr'                    => 'Predis_Commands_Increment',
+                'increment'           => 'Predis_Commands_Increment',
+            'incrby'                  => 'Predis_Commands_IncrementBy',
+                'incrementBy'         => 'Predis_Commands_IncrementBy',
+            'decr'                    => 'Predis_Commands_Decrement',
+                'decrement'           => 'Predis_Commands_Decrement',
+            'decrby'                  => 'Predis_Commands_DecrementBy',
+                'decrementBy'         => 'Predis_Commands_DecrementBy',
+            'exists'                  => 'Predis_Commands_Exists',
+            'del'                     => 'Predis_Commands_Delete',
+                'delete'              => 'Predis_Commands_Delete',
+            'type'                    => 'Predis_Commands_Type',
+
+            /* commands operating on the key space */
+            'keys'               => 'Predis_Commands_Keys',
+            'randomkey'          => 'Predis_Commands_RandomKey',
+                'randomKey'      => 'Predis_Commands_RandomKey',
+            'rename'             => 'Predis_Commands_Rename',
+            'renamenx'           => 'Predis_Commands_RenamePreserve',
+                'renamePreserve' => 'Predis_Commands_RenamePreserve',
+            'expire'             => 'Predis_Commands_Expire',
+            'expireat'           => 'Predis_Commands_ExpireAt',
+                'expireAt'       => 'Predis_Commands_ExpireAt',
+            'dbsize'             => 'Predis_Commands_DatabaseSize',
+                'databaseSize'   => 'Predis_Commands_DatabaseSize',
+            'ttl'                => 'Predis_Commands_TimeToLive',
+                'timeToLive'     => 'Predis_Commands_TimeToLive',
+
+            /* commands operating on lists */
+            'rpush'            => 'Predis_Commands_ListPushTail',
+                'pushTail'     => 'Predis_Commands_ListPushTail',
+            'lpush'            => 'Predis_Commands_ListPushHead',
+                'pushHead'     => 'Predis_Commands_ListPushHead',
+            'llen'             => 'Predis_Commands_ListLength',
+                'listLength'   => 'Predis_Commands_ListLength',
+            'lrange'           => 'Predis_Commands_ListRange',
+                'listRange'    => 'Predis_Commands_ListRange',
+            'ltrim'            => 'Predis_Commands_ListTrim',
+                'listTrim'     => 'Predis_Commands_ListTrim',
+            'lindex'           => 'Predis_Commands_ListIndex',
+                'listIndex'    => 'Predis_Commands_ListIndex',
+            'lset'             => 'Predis_Commands_ListSet',
+                'listSet'      => 'Predis_Commands_ListSet',
+            'lrem'             => 'Predis_Commands_ListRemove',
+                'listRemove'   => 'Predis_Commands_ListRemove',
+            'lpop'             => 'Predis_Commands_ListPopFirst',
+                'popFirst'     => 'Predis_Commands_ListPopFirst',
+            'rpop'             => 'Predis_Commands_ListPopLast',
+                'popLast'      => 'Predis_Commands_ListPopLast',
+            'rpoplpush'        => 'Predis_Commands_ListPopLastPushHead',
+                'listPopLastPushHead'  => 'Predis_Commands_ListPopLastPushHead',
+
+            /* commands operating on sets */
+            'sadd'                      => 'Predis_Commands_SetAdd', 
+                'setAdd'                => 'Predis_Commands_SetAdd',
+            'srem'                      => 'Predis_Commands_SetRemove', 
+                'setRemove'             => 'Predis_Commands_SetRemove',
+            'spop'                      => 'Predis_Commands_SetPop',
+                'setPop'                => 'Predis_Commands_SetPop',
+            'smove'                     => 'Predis_Commands_SetMove', 
+                'setMove'               => 'Predis_Commands_SetMove',
+            'scard'                     => 'Predis_Commands_SetCardinality', 
+                'setCardinality'        => 'Predis_Commands_SetCardinality',
+            'sismember'                 => 'Predis_Commands_SetIsMember', 
+                'setIsMember'           => 'Predis_Commands_SetIsMember',
+            'sinter'                    => 'Predis_Commands_SetIntersection', 
+                'setIntersection'       => 'Predis_Commands_SetIntersection',
+            'sinterstore'               => 'Predis_Commands_SetIntersectionStore', 
+                'setIntersectionStore'  => 'Predis_Commands_SetIntersectionStore',
+            'sunion'                    => 'Predis_Commands_SetUnion', 
+                'setUnion'              => 'Predis_Commands_SetUnion',
+            'sunionstore'               => 'Predis_Commands_SetUnionStore', 
+                'setUnionStore'         => 'Predis_Commands_SetUnionStore',
+            'sdiff'                     => 'Predis_Commands_SetDifference', 
+                'setDifference'         => 'Predis_Commands_SetDifference',
+            'sdiffstore'                => 'Predis_Commands_SetDifferenceStore', 
+                'setDifferenceStore'    => 'Predis_Commands_SetDifferenceStore',
+            'smembers'                  => 'Predis_Commands_SetMembers', 
+                'setMembers'            => 'Predis_Commands_SetMembers',
+            'srandmember'               => 'Predis_Commands_SetRandomMember', 
+                'setRandomMember'       => 'Predis_Commands_SetRandomMember',
+
+            /* commands operating on sorted sets */
+            'zadd'                          => 'Predis_Commands_ZSetAdd',
+                'zsetAdd'                   => 'Predis_Commands_ZSetAdd',
+            'zincrby'                       => 'Predis_Commands_ZSetIncrementBy',
+                'zsetIncrementBy'           => 'Predis_Commands_ZSetIncrementBy',
+            'zrem'                          => 'Predis_Commands_ZSetRemove',
+                'zsetRemove'                => 'Predis_Commands_ZSetRemove',
+            'zrange'                        => 'Predis_Commands_ZSetRange',
+                'zsetRange'                 => 'Predis_Commands_ZSetRange',
+            'zrevrange'                     => 'Predis_Commands_ZSetReverseRange',
+                'zsetReverseRange'          => 'Predis_Commands_ZSetReverseRange',
+            'zrangebyscore'                 => 'Predis_Commands_ZSetRangeByScore',
+                'zsetRangeByScore'          => 'Predis_Commands_ZSetRangeByScore',
+            'zcard'                         => 'Predis_Commands_ZSetCardinality',
+                'zsetCardinality'           => 'Predis_Commands_ZSetCardinality',
+            'zscore'                        => 'Predis_Commands_ZSetScore',
+                'zsetScore'                 => 'Predis_Commands_ZSetScore',
+            'zremrangebyscore'              => 'Predis_Commands_ZSetRemoveRangeByScore',
+                'zsetRemoveRangeByScore'    => 'Predis_Commands_ZSetRemoveRangeByScore',
+
+            /* multiple databases handling commands */
+            'select'                => 'Predis_Commands_SelectDatabase', 
+                'selectDatabase'    => 'Predis_Commands_SelectDatabase',
+            'move'                  => 'Predis_Commands_MoveKey', 
+                'moveKey'           => 'Predis_Commands_MoveKey',
+            'flushdb'               => 'Predis_Commands_FlushDatabase', 
+                'flushDatabase'     => 'Predis_Commands_FlushDatabase',
+            'flushall'              => 'Predis_Commands_FlushAll', 
+                'flushDatabases'    => 'Predis_Commands_FlushAll',
+
+            /* sorting */
+            'sort'                  => 'Predis_Commands_Sort',
+
+            /* remote server control commands */
+            'info'                  => 'Predis_Commands_Info',
+            'slaveof'               => 'Predis_Commands_SlaveOf', 
+                'slaveOf'           => 'Predis_Commands_SlaveOf',
+
+            /* persistence control commands */
+            'save'                  => 'Predis_Commands_Save',
+            'bgsave'                => 'Predis_Commands_BackgroundSave', 
+                'backgroundSave'    => 'Predis_Commands_BackgroundSave',
+            'lastsave'              => 'Predis_Commands_LastSave', 
+                'lastSave'          => 'Predis_Commands_LastSave',
+            'shutdown'              => 'Predis_Commands_Shutdown',
+            'bgrewriteaof'                      =>  'Predis_Commands_BackgroundRewriteAppendOnlyFile',
+            'backgroundRewriteAppendOnlyFile'   =>  'Predis_Commands_BackgroundRewriteAppendOnlyFile',
+        );
+    }
+}
+
+class Predis_RedisServer_v2_0 extends Predis_RedisServer_v1_2 {
+    public function getVersion() { return '2.0'; }
+    public function getSupportedCommands() {
+        return array_merge(parent::getSupportedCommands(), array(
+            /* transactions */
+            'multi'                     => 'Predis_Commands_Multi',
+            'exec'                      => 'Predis_Commands_Exec',
+            'discard'                   => 'Predis_Commands_Discard',
+
+            /* commands operating on string values */
+            'setex'                     => 'Predis_Commands_SetExpire',
+                'setExpire'             => 'Predis_Commands_SetExpire',
+            'append'                    => 'Predis_Commands_Append',
+            'substr'                    => 'Predis_Commands_Substr',
+
+            /* commands operating on lists */
+            'blpop'                     => 'Predis_Commands_ListPopFirstBlocking',
+                'popFirstBlocking'      => 'Predis_Commands_ListPopFirstBlocking',
+            'brpop'                     => 'Predis_Commands_ListPopLastBlocking',
+                'popLastBlocking'       => 'Predis_Commands_ListPopLastBlocking',
+
+            /* commands operating on sorted sets */
+            'zunionstore'               => 'Predis_Commands_ZSetUnionStore',
+                'zsetUnionStore'        => 'Predis_Commands_ZSetUnionStore',
+            'zinterstore'               => 'Predis_Commands_ZSetIntersectionStore',
+                'zsetIntersectionStore' => 'Predis_Commands_ZSetIntersectionStore',
+            'zcount'                    => 'Predis_Commands_ZSetCount',
+                'zsetCount'             => 'Predis_Commands_ZSetCount',
+            'zrank'                     => 'Predis_Commands_ZSetRank',
+                'zsetRank'              => 'Predis_Commands_ZSetRank',
+            'zrevrank'                  => 'Predis_Commands_ZSetReverseRank',
+                'zsetReverseRank'       => 'Predis_Commands_ZSetReverseRank',
+            'zremrangebyrank'           => 'Predis_Commands_ZSetRemoveRangeByRank',
+                'zsetRemoveRangeByRank' => 'Predis_Commands_ZSetRemoveRangeByRank',
+
+            /* commands operating on hashes */
+            'hset'                      => 'Predis_Commands_HashSet',
+                'hashSet'               => 'Predis_Commands_HashSet',
+            'hsetnx'                    => 'Predis_Commands_HashSetPreserve',
+                'hashSetPreserve'       => 'Predis_Commands_HashSetPreserve',
+            'hmset'                     => 'Predis_Commands_HashSetMultiple',
+                'hashSetMultiple'       => 'Predis_Commands_HashSetMultiple',
+            'hincrby'                   => 'Predis_Commands_HashIncrementBy',
+                'hashIncrementBy'       => 'Predis_Commands_HashIncrementBy',
+            'hget'                      => 'Predis_Commands_HashGet',
+                'hashGet'               => 'Predis_Commands_HashGet',
+            'hmget'                     => 'Predis_Commands_HashGetMultiple',
+                'hashGetMultiple'       => 'Predis_Commands_HashGetMultiple',
+            'hdel'                      => 'Predis_Commands_HashDelete',
+                'hashDelete'            => 'Predis_Commands_HashDelete',
+            'hexists'                   => 'Predis_Commands_HashExists',
+                'hashExists'            => 'Predis_Commands_HashExists',
+            'hlen'                      => 'Predis_Commands_HashLength',
+                'hashLength'            => 'Predis_Commands_HashLength',
+            'hkeys'                     => 'Predis_Commands_HashKeys',
+                'hashKeys'              => 'Predis_Commands_HashKeys',
+            'hvals'                     => 'Predis_Commands_HashValues',
+                'hashValues'            => 'Predis_Commands_HashValues',
+            'hgetall'                   => 'Predis_Commands_HashGetAll',
+                'hashGetAll'            => 'Predis_Commands_HashGetAll',
+
+            /* publish - subscribe */
+            'subscribe'                 => 'Predis_Commands_Subscribe',
+            'unsubscribe'               => 'Predis_Commands_Unsubscribe',
+            'psubscribe'                => 'Predis_Commands_SubscribeByPattern',
+            'punsubscribe'              => 'Predis_Commands_UnsubscribeByPattern',
+            'publish'                   => 'Predis_Commands_Publish',
+
+            /* remote server control commands */
+            'config'                    => 'Predis_Commands_Config',
+                'configuration'         => 'Predis_Commands_Config',
+        ));
+    }
+}
+
+class Predis_RedisServer_vNext extends Predis_RedisServer_v2_0 {
+    public function getVersion() { return '2.1'; }
+    public function getSupportedCommands() {
+        return array_merge(parent::getSupportedCommands(), array(
+            /* transactions */
+            'watch'                     => 'Predis_Commands_Watch',
+            'unwatch'                   => 'Predis_Commands_Unwatch',
+
+            /* commands operating on string values */
+            'strlen'                    => 'Predis_Commands_Strlen',
+            'setrange'                  => 'Predis_Commands_SetRange',
+            'getrange'                  => 'Predis_Commands_Substr',
+            'setbit'                    => 'Predis_Commands_SetBit',
+            'getbit'                    => 'Predis_Commands_GetBit',
+
+            /* commands operating on the key space */
+            'persist'                   => 'Predis_Commands_Persist',
+
+            /* commands operating on lists */
+            'rpushx'                    => 'Predis_Commands_ListPushTailX',
+            'lpushx'                    => 'Predis_Commands_ListPushHeadX',
+            'linsert'                   => 'Predis_Commands_ListInsert',
+            'brpoplpush'                => 'Predis_Commands_ListPopLastPushHeadBlocking',
+
+            /* commands operating on sorted sets */
+            'zrevrangebyscore'          => 'Predis_Commands_ZSetReverseRangeByScore',
+        ));
+    }
+}
+
+/* ------------------------------------------------------------------------- */