From 2a651218657a5bd8f2febcf3c56aee0fbd7a6b14 Mon Sep 17 00:00:00 2001 From: Marcel Beerta Date: Sun, 19 Feb 2012 08:43:28 +0100 Subject: [PATCH 1/5] Fix several issues in MemccheSessionStorage which prevented it from being used correctly --- .../Session/Storage/MemcacheSessionStorage.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) mode change 100644 => 100755 src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php old mode 100644 new mode 100755 index 3064db3bbb..94eb1f19ba --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php @@ -72,14 +72,18 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionHa protected function addServer(array $server) { - if (array_key_exists('host', $server)) { + if (!array_key_exists('host', $server)) { throw new \InvalidArgumentException('host key must be set'); } $server['port'] = isset($server['port']) ? (int)$server['port'] : 11211; $server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1; - $server['presistent'] = isset($server['presistent']) ? (bool)$server['presistent'] : false; - $server['weight'] = isset($server['weight']) ? (bool)$server['weight'] : 1; + $server['persistent'] = isset($server['persistent']) ? (bool)$server['persistent'] : false; + $server['weight'] = isset($server['weight']) ? (int)$server['weight'] : 1; + $server['retry_interval'] = isset($server['retry_interval']) ? (int)$server['retry_interval'] : 15; + + $this->memcache->addserver($server['host'], $server['port'], $server['persistent'],$server['weight'],$server['timeout'],$server['retry_interval']); + } /** @@ -88,7 +92,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionHa public function open($savePath, $sessionName) { foreach ($this->memcacheOptions['serverpool'] as $server) { - $this->memcache->addServer($server); + $this->addServer($server); } return true; From 0e0141805c5fa209108616ab6b9c68806943eb31 Mon Sep 17 00:00:00 2001 From: Marcel Beerta Date: Sun, 19 Feb 2012 08:56:31 +0100 Subject: [PATCH 2/5] Fix default if no serverpool is provided --- .../Session/Storage/MemcacheSessionStorage.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php index 94eb1f19ba..df5f6fd98e 100755 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php @@ -54,12 +54,14 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionHa // defaults if (!isset($memcacheOptions['serverpool'])) { - $memcacheOptions['serverpool'] = array( + $memcacheOptions['serverpool'] = array(array( 'host' => '127.0.0.1', 'port' => 11211, 'timeout' => 1, 'persistent' => false, - 'weight' => 1); + 'weight' => 1, + 'retry_interval' => 15 + )); } $memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400; From 3dd851afed8d7683e23d21bb2ea2c2fcf40bc291 Mon Sep 17 00:00:00 2001 From: Marcel Beerta Date: Sun, 19 Feb 2012 13:22:38 +0100 Subject: [PATCH 3/5] Use correct parameters --- .../HttpFoundation/Session/Storage/MemcacheSessionStorage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php index df5f6fd98e..4fa9ccbfa0 100755 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php @@ -121,7 +121,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionHa */ public function write($sessionId, $data) { - return $this->memcache->set($this->prefix.$sessionId, $data, $this->memcacheOptions['expiretime']); + return $this->memcache->set($this->prefix.$sessionId, $data, 0, $this->memcacheOptions['expiretime']); } /** From b4c53238b00cad58ed4cbc8f53bf1365448ce8e5 Mon Sep 17 00:00:00 2001 From: Marcel Beerta Date: Sun, 19 Feb 2012 19:31:41 +0100 Subject: [PATCH 4/5] Added comma to array initializer, reverted permissions back to 644 --- .../HttpFoundation/Session/Storage/MemcacheSessionStorage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php old mode 100755 new mode 100644 index 4fa9ccbfa0..70be3e913c --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MemcacheSessionStorage.php @@ -60,7 +60,7 @@ class MemcacheSessionStorage extends AbstractSessionStorage implements SessionHa 'timeout' => 1, 'persistent' => false, 'weight' => 1, - 'retry_interval' => 15 + 'retry_interval' => 15, )); } From 6fbd2902be169edb93a059225f1778196299d4ea Mon Sep 17 00:00:00 2001 From: Marcel Beerta Date: Sun, 19 Feb 2012 19:54:54 +0100 Subject: [PATCH 5/5] Improved unit tests for MemcacheSessionStorage --- .../Storage/MemcacheSessionStorageTest.php | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MemcacheSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MemcacheSessionStorageTest.php index a8327e0f9c..1cdbe2a3a9 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MemcacheSessionStorageTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/MemcacheSessionStorageTest.php @@ -32,11 +32,48 @@ class MemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase public function testOpenSession() { $this->memcache->expects($this->atLeastOnce()) - ->method('addServer'); + ->method('addServer') + ->with('127.0.0.1', 11211, false, 1, 1, 15); $this->assertTrue($this->storage->open('', '')); } + public function testConstructingWithServerPool() + { + $mock = $this->getMock('Memcache'); + + $storage = new MemcacheSessionStorage($mock, array( + 'serverpool' => array( + array('host' => '127.0.0.2'), + array('host' => '127.0.0.3', + 'port' => 11212, + 'timeout' => 10, + 'persistent' => true, + 'weight' => 5, + 'retry_interval' => 39, + ), + array('host' => '127.0.0.4', + 'port' => 11211, + 'weight' => 2 + ), + ), + )); + + $matcher = $mock + ->expects($this->at(0)) + ->method('addServer') + ->with('127.0.0.2', 11211, false, 1, 1, 15); + $matcher = $mock + ->expects($this->at(1)) + ->method('addServer') + ->with('127.0.0.3', 11212, true, 5, 10, 39); + $matcher = $mock + ->expects($this->at(2)) + ->method('addServer') + ->with('127.0.0.4', 11211, false, 2, 1, 15); + $this->assertTrue($storage->open('', '')); + } + public function testCloseSession() { $this->memcache->expects($this->once())