Skip to content

Commit

Permalink
[HttpFoundation][Session] Assume that memcache(d) instances are alrea…
Browse files Browse the repository at this point in the history
…dy configured
  • Loading branch information
vicb committed May 8, 2012
1 parent 72d21c6 commit 0216e05
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 124 deletions.
Expand Up @@ -44,92 +44,59 @@ 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()
{
return $this->memcache->close();
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function read($sessionId)
{
return $this->memcache->get($this->prefix.$sessionId) ?: '';
}

/**
* {@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)
{
return $this->memcache->delete($this->prefix.$sessionId);
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function gc($lifetime)
{
Expand Down
Expand Up @@ -37,94 +37,75 @@ 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()
{
return true;
}

/**
* {@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;
}
}
Expand Up @@ -40,49 +40,9 @@ protected function tearDown()

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())
Expand Down
Expand Up @@ -40,10 +40,6 @@ protected function tearDown()

public function testOpenSession()
{
$this->memcached->expects($this->atLeastOnce())
->method('addServers')
->will($this->returnValue(true));

$this->assertTrue($this->storage->open('', ''));
}

Expand Down

0 comments on commit 0216e05

Please sign in to comment.