From 0216e056051a6304cab01defc107fb583f3f5efa Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 4 May 2012 17:48:17 +0200 Subject: [PATCH] [HttpFoundation][Session] Assume that memcache(d) instances are already configured --- .../Handler/MemcacheSessionHandler.php | 49 +++------------ .../Handler/MemcachedSessionHandler.php | 59 +++++++------------ .../Handler/MemcacheSessionHandlerTest.php | 40 ------------- .../Handler/MemcachedSessionHandlerTest.php | 4 -- 4 files changed, 28 insertions(+), 124 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcacheSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcacheSessionHandler.php index 00488fd0d9..1220cf6598 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcacheSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcacheSessionHandler.php @@ -44,60 +44,27 @@ class MemcacheSessionHandler implements \SessionHandlerInterface * * @param \Memcache $memcache A \Memcache instance * @param array $memcacheOptions An associative array of Memcache options - * @param array $options Session configuration options. */ - public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array()) + public function __construct(\Memcache $memcache, array $memcacheOptions = array()) { $this->memcache = $memcache; - // defaults - if (!isset($memcacheOptions['serverpool'])) { - $memcacheOptions['serverpool'] = array(array( - 'host' => '127.0.0.1', - 'port' => 11211, - 'timeout' => 1, - 'persistent' => false, - 'weight' => 1, - 'retry_interval' => 15, - )); - } - $memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400; $this->prefix = isset($memcacheOptions['prefix']) ? $memcacheOptions['prefix'] : 'sf2s'; $this->memcacheOptions = $memcacheOptions; } - protected function addServer(array $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['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']); - - } - /** - * {@inheritdoc} + * {@inheritDoc} */ public function open($savePath, $sessionName) { - foreach ($this->memcacheOptions['serverpool'] as $server) { - $this->addServer($server); - } - return true; } /** - * {@inheritdoc} + * {@inheritDoc} */ public function close() { @@ -105,7 +72,7 @@ class MemcacheSessionHandler implements \SessionHandlerInterface } /** - * {@inheritdoc} + * {@inheritDoc} */ public function read($sessionId) { @@ -113,15 +80,15 @@ class MemcacheSessionHandler implements \SessionHandlerInterface } /** - * {@inheritdoc} + * {@inheritDoc} */ public function write($sessionId, $data) { - return $this->memcache->set($this->prefix.$sessionId, $data, 0, $this->memcacheOptions['expiretime']); + return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->memcacheOptions['expiretime']); } /** - * {@inheritdoc} + * {@inheritDoc} */ public function destroy($sessionId) { @@ -129,7 +96,7 @@ class MemcacheSessionHandler implements \SessionHandlerInterface } /** - * {@inheritdoc} + * {@inheritDoc} */ public function gc($lifetime) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php index 71770dda83..83afbd27ab 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php @@ -37,42 +37,39 @@ class MemcachedSessionHandler implements \SessionHandlerInterface */ private $memcachedOptions; + /** + * Key prefix for shared environments. + * + * @var string + */ + private $prefix; + /** * Constructor. * * @param \Memcached $memcached A \Memcached instance * @param array $memcachedOptions An associative array of Memcached options - * @param array $options Session configuration options. */ - public function __construct(\Memcached $memcached, array $memcachedOptions = array(), array $options = array()) + public function __construct(\Memcached $memcached, array $memcachedOptions = array()) { $this->memcached = $memcached; - // defaults - if (!isset($memcachedOptions['serverpool'])) { - $memcachedOptions['serverpool'][] = array( - 'host' => '127.0.0.1', - 'port' => 11211, - 'weight' => 1); - } - - $memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int)$memcachedOptions['expiretime'] : 86400; - - $this->memcached->setOption(\Memcached::OPT_PREFIX_KEY, isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s'); + $memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int) $memcachedOptions['expiretime'] : 86400; + $this->prefix = isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s'; $this->memcachedOptions = $memcachedOptions; } /** - * {@inheritdoc} + * {@inheritDoc} */ public function open($savePath, $sessionName) { - return $this->memcached->addServers($this->memcachedOptions['serverpool']); + return true; } /** - * {@inheritdoc} + * {@inheritDoc} */ public function close() { @@ -80,51 +77,35 @@ class MemcachedSessionHandler implements \SessionHandlerInterface } /** - * {@inheritdoc} + * {@inheritDoc} */ public function read($sessionId) { - return $this->memcached->get($sessionId) ?: ''; + return $this->memcached->get($this->prefix.$sessionId) ?: ''; } /** - * {@inheritdoc} + * {@inheritDoc} */ public function write($sessionId, $data) { - return $this->memcached->set($sessionId, $data, $this->memcachedOptions['expiretime']); + return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->memcachedOptions['expiretime']); } /** - * {@inheritdoc} + * {@inheritDoc} */ public function destroy($sessionId) { - return $this->memcached->delete($sessionId); + return $this->memcached->delete($this->prefix.$sessionId); } /** - * {@inheritdoc} + * {@inheritDoc} */ public function gc($lifetime) { // not required here because memcached will auto expire the records anyhow. return true; } - - /** - * Adds a server to the memcached handler. - * - * @param array $server - */ - protected function addServer(array $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; - } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcacheSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcacheSessionHandlerTest.php index 49e1568a09..ae1002b2dd 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcacheSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcacheSessionHandlerTest.php @@ -40,49 +40,9 @@ class MemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase public function testOpenSession() { - $this->memcache->expects($this->atLeastOnce()) - ->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 MemcacheSessionHandler($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()) diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php index 5731d92c22..f47380fcd6 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MemcachedSessionHandlerTest.php @@ -40,10 +40,6 @@ class MemcacheddSessionHandlerTest extends \PHPUnit_Framework_TestCase public function testOpenSession() { - $this->memcached->expects($this->atLeastOnce()) - ->method('addServers') - ->will($this->returnValue(true)); - $this->assertTrue($this->storage->open('', '')); }