Bladeren bron

Make it possible to serialize and unserialize connection instances.

Daniele Alessandri 13 jaren geleden
bovenliggende
commit
93163bdfb3

+ 8 - 0
lib/Predis/Network/ComposableStreamConnection.php

@@ -132,4 +132,12 @@ class ComposableStreamConnection extends StreamConnection implements IConnection
     {
         return $this->protocol->read($this);
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __sleep()
+    {
+        return array_merge(parent::__sleep(), array('protocol'));
+    }
 }

+ 8 - 0
lib/Predis/Network/ConnectionBase.php

@@ -232,4 +232,12 @@ abstract class ConnectionBase implements IConnectionSingle
 
         return $this->cachedId;
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __sleep()
+    {
+        return array('parameters', 'initCmds');
+    }
 }

+ 8 - 0
lib/Predis/Network/PhpiredisConnection.php

@@ -384,4 +384,12 @@ class PhpiredisConnection extends ConnectionBase
         array_unshift($cmdargs, $command->getId());
         $this->write(phpiredis_format_command($cmdargs));
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __wakeup()
+    {
+        $this->initializeProtocol($this->getParameters());
+    }
 }

+ 8 - 0
lib/Predis/Network/StreamConnection.php

@@ -284,4 +284,12 @@ class StreamConnection extends ConnectionBase
 
         $this->writeBytes($buffer);
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __sleep()
+    {
+        return array_merge(parent::__sleep(), array('mbiterable', 'throwErrors'));
+    }
 }

+ 20 - 0
lib/Predis/Network/WebdisConnection.php

@@ -321,4 +321,24 @@ class WebdisConnection implements IConnectionSingle
     {
         return "{$this->parameters->host}:{$this->parameters->port}";
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __sleep()
+    {
+        return array('parameters');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function __wakeup()
+    {
+        $this->checkExtensions();
+        $parameters = $this->getParameters();
+
+        $this->resource = $this->initializeCurl($parameters);
+        $this->reader = $this->initializeReader($parameters);
+    }
 }

+ 14 - 0
tests/Predis/Network/PhpiredisConnectionTest.php

@@ -53,6 +53,20 @@ class PhpiredisConnectionTest extends ConnectionTestCase
         $connection = new PhpiredisConnection($parameters);
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testCanBeSerialized()
+    {
+        $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
+        $connection = new PhpiredisConnection($parameters);
+
+        $unserialized = unserialize(serialize($connection));
+
+        $this->assertInstanceOf('Predis\Network\PhpiredisConnection', $unserialized);
+        $this->assertEquals($parameters, $unserialized->getParameters());
+    }
+
     // ******************************************************************** //
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //

+ 19 - 0
tests/Predis/Network/PredisClusterTest.php

@@ -321,6 +321,25 @@ class PredisClusterTest extends StandardTestCase
         $cluster->executeCommand($command);
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testCanBeSerialized()
+    {
+        $connection1 = $this->getMockConnection('tcp://host1?alias=first');
+        $connection2 = $this->getMockConnection('tcp://host2?alias=second');
+
+        $cluster = new PredisCluster();
+        $cluster->add($connection1);
+        $cluster->add($connection2);
+
+        // We use the following line to initialize the underlying hashring.
+        $cluster->getConnectionByKey('foo');
+        $unserialized = unserialize(serialize($cluster));
+
+        $this->assertEquals($cluster, $unserialized);
+    }
+
     // ******************************************************************** //
     // ---- HELPER METHODS ------------------------------------------------ //
     // ******************************************************************** //

+ 13 - 0
tests/Predis/Network/StreamConnectionTest.php

@@ -53,6 +53,19 @@ class StreamConnectionTest extends ConnectionTestCase
         $connection = new StreamConnection($parameters);
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testCanBeSerialized()
+    {
+        $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
+        $connection = new StreamConnection($parameters);
+
+        $unserialized = unserialize(serialize($connection));
+
+        $this->assertEquals($connection, $unserialized);
+    }
+
     // ******************************************************************** //
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //

+ 14 - 0
tests/Predis/Network/WebdisConnectionTest.php

@@ -100,6 +100,20 @@ class WebdisConnectionTest extends StandardTestCase
         $connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar')));
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testCanBeSerialized()
+    {
+        $parameters = $this->getParameters(array('alias' => 'webdis'));
+        $connection = new WebdisConnection($parameters);
+
+        $unserialized = unserialize(serialize($connection));
+
+        $this->assertInstanceOf('Predis\Network\WebdisConnection', $unserialized);
+        $this->assertEquals($parameters, $unserialized->getParameters());
+    }
+
     // ******************************************************************** //
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //