diff --git a/lib/Predis/Network/ConnectionBase.php b/lib/Predis/Network/ConnectionBase.php index cd181e38..c73a5318 100644 --- a/lib/Predis/Network/ConnectionBase.php +++ b/lib/Predis/Network/ConnectionBase.php @@ -9,8 +9,8 @@ use Predis\CommunicationException; use Predis\Commands\ICommand; abstract class ConnectionBase implements IConnectionSingle { - private $_cachedId; - protected $_params, $_initCmds, $_resource; + private $_cachedId, $_resource; + protected $_params, $_initCmds; public function __construct(ConnectionParameters $parameters) { $this->_initCmds = array(); @@ -22,7 +22,7 @@ abstract class ConnectionBase implements IConnectionSingle { } public function isConnected() { - return is_resource($this->_resource); + return isset($this->_resource); } protected abstract function createResource(); @@ -34,6 +34,10 @@ abstract class ConnectionBase implements IConnectionSingle { $this->_resource = $this->createResource(); } + public function disconnect() { + unset($this->_resource); + } + public function pushInitCommand(ICommand $command){ $this->_initCmds[] = $command; } @@ -50,9 +54,10 @@ abstract class ConnectionBase implements IConnectionSingle { } public function getResource() { - if (!$this->isConnected()) { - $this->connect(); + if (isset($this->_resource)) { + return $this->_resource; } + $this->connect(); return $this->_resource; } diff --git a/lib/Predis/Network/PhpiredisConnection.php b/lib/Predis/Network/PhpiredisConnection.php index ca9c7772..6fedad33 100644 --- a/lib/Predis/Network/PhpiredisConnection.php +++ b/lib/Predis/Network/PhpiredisConnection.php @@ -40,8 +40,8 @@ class PhpiredisConnection extends ConnectionBase { } public function __destruct() { - $this->disconnect(); phpiredis_reader_destroy($this->_reader); + parent::__destruct(); } protected function checkParameters(ConnectionParameters $parameters) { @@ -202,8 +202,8 @@ class PhpiredisConnection extends ConnectionBase { public function disconnect() { if ($this->isConnected()) { - socket_close($this->_resource); - $this->_resource = null; + socket_close($this->getResource()); + parent::disconnect(); } } diff --git a/lib/Predis/Network/StreamConnection.php b/lib/Predis/Network/StreamConnection.php index 80ea10b2..dafcd3c5 100644 --- a/lib/Predis/Network/StreamConnection.php +++ b/lib/Predis/Network/StreamConnection.php @@ -98,6 +98,7 @@ class StreamConnection extends ConnectionBase { public function disconnect() { if ($this->isConnected()) { fclose($this->getResource()); + parent::disconnect(); } }