From bc56ea9ecaa57186c55ab799d6e099cc3169488b Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 2 Apr 2011 14:48:07 +0400 Subject: [PATCH 1/5] transaction can executed without commands --- library/Rediska/Transaction.php | 34 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/library/Rediska/Transaction.php b/library/Rediska/Transaction.php index 5b64e8a..63228af 100644 --- a/library/Rediska/Transaction.php +++ b/library/Rediska/Transaction.php @@ -120,32 +120,30 @@ public function execute() { $results = array(); - if (!empty($this->_commands)) { - $this->_rediska->getProfiler()->start($this); + $this->_rediska->getProfiler()->start($this); - $multi = new Rediska_Connection_Exec($this->_connection, 'MULTI'); - $multi->execute(); + $multi = new Rediska_Connection_Exec($this->_connection, 'MULTI'); + $multi->execute(); - foreach($this->_commands as $command) { - $command->execute(); - } - - $exec = new Rediska_Connection_Exec($this->_connection, 'EXEC'); - $responses = $exec->execute(); + foreach($this->_commands as $command) { + $command->execute(); + } - $this->_rediska->getProfiler()->stop(); + $exec = new Rediska_Connection_Exec($this->_connection, 'EXEC'); + $responses = $exec->execute(); - if (!$responses) { - throw new Rediska_Transaction_AbortedException('Transaction has been aborted by server'); - } + $this->_rediska->getProfiler()->stop(); - foreach($this->_commands as $i => $command) { - $results[] = $command->parseResponses(array($responses[$i])); - } + if (!$responses) { + throw new Rediska_Transaction_AbortedException('Transaction has been aborted by server'); + } - $this->_reset(); + foreach($this->_commands as $i => $command) { + $results[] = $command->parseResponses(array($responses[$i])); } + $this->_reset(); + return $results; } From 239a6b3642fc88283bbe125b96d3e2556f3961ac Mon Sep 17 00:00:00 2001 From: till Date: Sat, 9 Apr 2011 17:24:06 +0200 Subject: [PATCH 2/5] * fix naming isWrited >> isWritten --- library/Rediska/Command/Abstract.php | 10 +++++----- library/Rediska/Connection/Exec.php | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/library/Rediska/Command/Abstract.php b/library/Rediska/Command/Abstract.php index 7283af0..9ab3c05 100644 --- a/library/Rediska/Command/Abstract.php +++ b/library/Rediska/Command/Abstract.php @@ -75,7 +75,7 @@ abstract class Rediska_Command_Abstract implements Rediska_Command_Interface * * @var unknown_type */ - protected $_isWrited = false; + protected $_isWritten = false; /** * Is queued to transaction @@ -135,7 +135,7 @@ public function write() $exec->write(); } - $this->_isWrited = true; + $this->_isWritten = true; return true; } @@ -149,7 +149,7 @@ public function read() { $responses = array(); - if (!$this->_isWrited) { + if (!$this->_isWritten) { throw new Rediska_Command_Exception('You need write before'); } @@ -162,7 +162,7 @@ public function read() return true; } else { - $this->_isWrited = false; + $this->_isWritten = false; return $this->parseResponses($responses); } } @@ -411,4 +411,4 @@ protected function _throwExceptionIfNotSupported($version = null) throw new Rediska_Command_Exception("Command '{$this->_name}' requires {$version}+ version of Redis server. Current version is {$redisVersion}. To change it specify 'redisVersion' option."); } } -} \ No newline at end of file +} diff --git a/library/Rediska/Connection/Exec.php b/library/Rediska/Connection/Exec.php index 0c86bc8..27e20c2 100644 --- a/library/Rediska/Connection/Exec.php +++ b/library/Rediska/Connection/Exec.php @@ -42,9 +42,9 @@ class Rediska_Connection_Exec /** * Is writed * - * @var $_isWrited string + * @var $_isWritten boolean */ - protected $_isWrited = false; + protected $_isWritten = false; /** * Response callback @@ -84,7 +84,7 @@ public function __construct(Rediska_Connection $connection, $command) public function write() { $result = $this->getConnection()->write($this->getCommand()); - $this->_isWrited = true; + $this->_isWritten = true; return $result; } @@ -94,9 +94,9 @@ public function write() * * @return boolean */ - public function isWrited() + public function isWritten() { - return $this->_isWrited; + return $this->_isWritten; } /** @@ -106,11 +106,11 @@ public function isWrited() */ public function read() { - if (!$this->isWrited()) { + if (!$this->isWritten()) { throw new Rediska_Connection_Exec_Exception('You must write command before read'); } - $this->_isWrited = false; + $this->_isWritten = false; if ($this->getResponseIterator() !== null) { if ($this->getResponseIterator() === true) { @@ -304,4 +304,4 @@ public static function readResponseFromConnection(Rediska_Connection $connection throw new Rediska_Connection_Exec_Exception("Invalid reply type: '$type'"); } } -} \ No newline at end of file +} From 2ce5df651113d35960d2df22631de09c7c37b1ac Mon Sep 17 00:00:00 2001 From: till Date: Sat, 9 Apr 2011 17:47:43 +0200 Subject: [PATCH 3/5] * support stream context --- library/Rediska/Connection.php | 49 +++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/library/Rediska/Connection.php b/library/Rediska/Connection.php index fdd78a3..6282598 100644 --- a/library/Rediska/Connection.php +++ b/library/Rediska/Connection.php @@ -41,16 +41,17 @@ class Rediska_Connection extends Rediska_Options * @var array */ protected $_options = array( - 'host' => self::DEFAULT_HOST, - 'port' => self::DEFAULT_PORT, - 'db' => self::DEFAULT_DB, - 'alias' => null, - 'weight' => self::DEFAULT_WEIGHT, - 'password' => null, - 'persistent' => false, - 'timeout' => null, - 'readTimeout' => null, - 'blockingMode' => true, + 'host' => self::DEFAULT_HOST, + 'port' => self::DEFAULT_PORT, + 'db' => self::DEFAULT_DB, + 'alias' => null, + 'weight' => self::DEFAULT_WEIGHT, + 'password' => null, + 'persistent' => false, + 'timeout' => null, + 'readTimeout' => null, + 'blockingMode' => true, + 'streamContext' => null, ); /** @@ -58,6 +59,7 @@ class Rediska_Connection extends Rediska_Options * * @throws Rediska_Connection_Exception * @return boolean + * @uses self::getStreamContext() */ public function connect() { @@ -70,7 +72,14 @@ public function connect() $flag = STREAM_CLIENT_CONNECT; } - $this->_socket = @stream_socket_client($socketAddress, $errno, $errmsg, $this->getTimeout(), $flag); + $this->_socket = @stream_socket_client( + $socketAddress, + $errno, + $errmsg, + $this->getTimeout(), + $flag, + $this->getStreamContext() + ); // Throw exception if can't connect if (!is_resource($this->_socket)) { @@ -345,6 +354,22 @@ public function getAlias() } } + /** + * If a stream context is provided, use it creating the socket. + * + * @return mixed null or resource + * @see self::connect() + */ + public function getStreamContext() + { + if ($this->_options['streamContext'] !== null + && is_resource($this->_options['streamContext']) + ) { + return $this->_options['streamContext']; + } + return null; + } + /** * Read and throw exception if somthing wrong * @@ -405,4 +430,4 @@ public function __clone() { $this->_socket = null; } -} \ No newline at end of file +} From d7cb099bcd87e5daea2f30dde3e9a18e32c43a75 Mon Sep 17 00:00:00 2001 From: till Date: Sat, 9 Apr 2011 17:51:22 +0200 Subject: [PATCH 4/5] * support both: array of options (e.g. bindto, backlog) or a 'resource' --- library/Rediska/Connection.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/library/Rediska/Connection.php b/library/Rediska/Connection.php index 6282598..3ce6178 100644 --- a/library/Rediska/Connection.php +++ b/library/Rediska/Connection.php @@ -357,15 +357,21 @@ public function getAlias() /** * If a stream context is provided, use it creating the socket. * + * It's supported to provide either an array with options, or an already created + * resource. + * * @return mixed null or resource * @see self::connect() */ public function getStreamContext() { - if ($this->_options['streamContext'] !== null - && is_resource($this->_options['streamContext']) - ) { - return $this->_options['streamContext']; + if ($this->_options['streamContext'] !== null) { + if (is_resource($this->_options['streamContext'])) { + return $this->_options['streamContext']; + } + if (is_array($this->_options['streamContext'])) { + return stream_context_create($this->_options['streamContext']); + } } return null; } From 91a5b8342cdef44b97218e04b716d63159e06cd5 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Wed, 13 Apr 2011 12:54:20 +0400 Subject: [PATCH 5/5] fix 10886, 10887 --- CHANGELOG.txt | 6 +++++- library/Rediska/PubSub/Channel.php | 4 +++- library/Rediska/Serializer/Adapter/Json.php | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 87945f1..bc878ac 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,4 +1,8 @@ -Version 0.5.5 (March 8, 2011): +Version 0.5.6 (April 13, 2011): + * Bug #10886: Remove json_last_error() from JSON serialzier + * Bug #10887: Subscribe don't unserialize values + +Version 0.5.5 (April 2, 2011): * Feature #4214: Profiler * Feature #9921: Implement SETBIT, GETBIT, SETRANGE, GETRANGE, STRLEN * Feature #9932: Implement LINSERT, LPUSHX, RPUSHX diff --git a/library/Rediska/PubSub/Channel.php b/library/Rediska/PubSub/Channel.php index 9403452..c9f597d 100644 --- a/library/Rediska/PubSub/Channel.php +++ b/library/Rediska/PubSub/Channel.php @@ -540,7 +540,9 @@ protected function _getResponseFromConnection(Rediska_Connection $connection) return new Rediska_PubSub_Response_Unsubscribe($connection, $channel); case self::MESSAGE: - return new Rediska_PubSub_Response_Message($connection, $channel, $body); + $message = $this->getRediska()->getSerializer()->unserialize($body); + + return new Rediska_PubSub_Response_Message($connection, $channel, $message); default: throw new Rediska_PubSub_Response_Exception('Unknown reponse type: ' . $type); diff --git a/library/Rediska/Serializer/Adapter/Json.php b/library/Rediska/Serializer/Adapter/Json.php index 2e6bbc1..0a02d91 100644 --- a/library/Rediska/Serializer/Adapter/Json.php +++ b/library/Rediska/Serializer/Adapter/Json.php @@ -87,9 +87,9 @@ public function serialize($value) */ public function unserialize($value) { - $value = json_decode($value); + $decodedValue = json_decode($value); - if (json_last_error() != JSON_ERROR_NONE) { + if ($decodedValue === null && $value !== 'null') { throw new Rediska_Serializer_Adapter_Exception("Can't unserialize value"); }