[Session] Memcache/d cleanup, test improvements
This commit is contained in:
parent
788adfb6c0
commit
12e22c0d1f
@ -19,40 +19,43 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
||||
class MemcacheSessionHandler implements \SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Memcache driver.
|
||||
*
|
||||
* @var \Memcache
|
||||
* @var \Memcache Memcache driver.
|
||||
*/
|
||||
private $memcache;
|
||||
|
||||
/**
|
||||
* Configuration options.
|
||||
*
|
||||
* @var array
|
||||
* @var integer Time to live in seconds
|
||||
*/
|
||||
private $memcacheOptions;
|
||||
private $ttl;
|
||||
|
||||
/**
|
||||
* Key prefix for shared environments.
|
||||
*
|
||||
* @var string
|
||||
* @var string Key prefix for shared environments.
|
||||
*/
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* List of available options:
|
||||
* * prefix: The prefix to use for the memcache keys in order to avoid collision
|
||||
* * expiretime: The time to live in seconds
|
||||
*
|
||||
* @param \Memcache $memcache A \Memcache instance
|
||||
* @param array $memcacheOptions An associative array of Memcache options
|
||||
* @param array $options An associative array of Memcache options
|
||||
*
|
||||
* @throws \InvalidArgumentException When unsupported options are passed
|
||||
*/
|
||||
public function __construct(\Memcache $memcache, array $memcacheOptions = array())
|
||||
public function __construct(\Memcache $memcache, array $options = array())
|
||||
{
|
||||
if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'The following options are not supported "%s"', implode(', ', $diff)
|
||||
));
|
||||
}
|
||||
|
||||
$this->memcache = $memcache;
|
||||
|
||||
$memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400;
|
||||
$this->prefix = isset($memcacheOptions['prefix']) ? $memcacheOptions['prefix'] : 'sf2s';
|
||||
|
||||
$this->memcacheOptions = $memcacheOptions;
|
||||
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
|
||||
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,7 +87,7 @@ class MemcacheSessionHandler implements \SessionHandlerInterface
|
||||
*/
|
||||
public function write($sessionId, $data)
|
||||
{
|
||||
return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->memcacheOptions['expiretime']);
|
||||
return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,40 +24,45 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
|
||||
class MemcachedSessionHandler implements \SessionHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Memcached driver.
|
||||
*
|
||||
* @var \Memcached
|
||||
* @var \Memcached Memcached driver.
|
||||
*/
|
||||
private $memcached;
|
||||
|
||||
/**
|
||||
* Configuration options.
|
||||
*
|
||||
* @var array
|
||||
* @var integer Time to live in seconds
|
||||
*/
|
||||
private $memcachedOptions;
|
||||
private $ttl;
|
||||
|
||||
|
||||
/**
|
||||
* Key prefix for shared environments.
|
||||
*
|
||||
* @var string
|
||||
* @var string Key prefix for shared environments.
|
||||
*/
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* List of available options:
|
||||
* * prefix: The prefix to use for the memcached keys in order to avoid collision
|
||||
* * expiretime: The time to live in seconds
|
||||
*
|
||||
* @param \Memcached $memcached A \Memcached instance
|
||||
* @param array $memcachedOptions An associative array of Memcached options
|
||||
* @param array $options An associative array of Memcached options
|
||||
*
|
||||
* @throws \InvalidArgumentException When unsupported options are passed
|
||||
*/
|
||||
public function __construct(\Memcached $memcached, array $memcachedOptions = array())
|
||||
public function __construct(\Memcached $memcached, array $options = array())
|
||||
{
|
||||
$this->memcached = $memcached;
|
||||
|
||||
$memcachedOptions['expiretime'] = isset($memcachedOptions['expiretime']) ? (int) $memcachedOptions['expiretime'] : 86400;
|
||||
$this->prefix = isset($memcachedOptions['prefix']) ? $memcachedOptions['prefix'] : 'sf2s';
|
||||
if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'The following options are not supported "%s"', implode(', ', $diff)
|
||||
));
|
||||
}
|
||||
|
||||
$this->memcachedOptions = $memcachedOptions;
|
||||
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
|
||||
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,7 +94,7 @@ class MemcachedSessionHandler implements \SessionHandlerInterface
|
||||
*/
|
||||
public function write($sessionId, $data)
|
||||
{
|
||||
return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->memcachedOptions['expiretime']);
|
||||
return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,6 +15,8 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHand
|
||||
|
||||
class MemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const PREFIX = 'prefix_';
|
||||
const TTL = 1000;
|
||||
/**
|
||||
* @var MemcacheSessionHandler
|
||||
*/
|
||||
@ -29,7 +31,10 @@ class MemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
$this->memcache = $this->getMock('Memcache');
|
||||
$this->storage = new MemcacheSessionHandler($this->memcache);
|
||||
$this->storage = new MemcacheSessionHandler(
|
||||
$this->memcache,
|
||||
array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
@ -45,37 +50,48 @@ class MemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testCloseSession()
|
||||
{
|
||||
$this->memcache->expects($this->once())
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('close')
|
||||
->will($this->returnValue(true));
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->close());
|
||||
}
|
||||
|
||||
public function testReadSession()
|
||||
{
|
||||
$this->memcache->expects($this->once())
|
||||
->method('get');
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with(self::PREFIX.'id')
|
||||
;
|
||||
|
||||
$this->assertEquals('', $this->storage->read(''));
|
||||
$this->assertEquals('', $this->storage->read('id'));
|
||||
}
|
||||
|
||||
public function testWriteSession()
|
||||
{
|
||||
$this->memcache->expects($this->once())
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
->with(self::PREFIX.'id', 'data', 0, $this->equalTo(time() + self::TTL, 2))
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->write('', ''));
|
||||
$this->assertTrue($this->storage->write('id', 'data'));
|
||||
}
|
||||
|
||||
public function testDestroySession()
|
||||
{
|
||||
$this->memcache->expects($this->once())
|
||||
$this->memcache
|
||||
->expects($this->once())
|
||||
->method('delete')
|
||||
->will($this->returnValue(true));
|
||||
->with(self::PREFIX.'id')
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->destroy(''));
|
||||
$this->assertTrue($this->storage->destroy('id'));
|
||||
}
|
||||
|
||||
public function testGcSession()
|
||||
@ -83,4 +99,26 @@ class MemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($this->storage->gc(123));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getOptionFixtures
|
||||
*/
|
||||
public function testSupportedOptions($options, $supported)
|
||||
{
|
||||
try {
|
||||
new MemcacheSessionHandler($this->memcache, $options);
|
||||
$this->assertTrue($supported);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertFalse($supported);
|
||||
}
|
||||
}
|
||||
|
||||
public function getOptionFixtures()
|
||||
{
|
||||
return array(
|
||||
array(array('prefix' => 'session'), true),
|
||||
array(array('expiretime' => 100), true),
|
||||
array(array('prefix' => 'session', 'expiretime' => 200), true),
|
||||
array(array('expiretime' => 100, 'foo' => 'bar'), false),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHan
|
||||
|
||||
class MemcacheddSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const PREFIX = 'prefix_';
|
||||
const TTL = 1000;
|
||||
|
||||
/**
|
||||
* @var MemcachedSessionHandler
|
||||
*/
|
||||
@ -29,7 +32,10 @@ class MemcacheddSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
$this->memcached = $this->getMock('Memcached');
|
||||
$this->storage = new MemcachedSessionHandler($this->memcached);
|
||||
$this->storage = new MemcachedSessionHandler(
|
||||
$this->memcached,
|
||||
array('prefix' => self::PREFIX, 'expiretime' => self::TTL)
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
@ -50,28 +56,37 @@ class MemcacheddSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testReadSession()
|
||||
{
|
||||
$this->memcached->expects($this->once())
|
||||
->method('get');
|
||||
$this->memcached
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with(self::PREFIX.'id')
|
||||
;
|
||||
|
||||
$this->assertEquals('', $this->storage->read(''));
|
||||
$this->assertEquals('', $this->storage->read('id'));
|
||||
}
|
||||
|
||||
public function testWriteSession()
|
||||
{
|
||||
$this->memcached->expects($this->once())
|
||||
$this->memcached
|
||||
->expects($this->once())
|
||||
->method('set')
|
||||
->will($this->returnValue(true));
|
||||
->with(self::PREFIX.'id', 'data', $this->equalTo(time() + self::TTL, 2))
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->write('', ''));
|
||||
$this->assertTrue($this->storage->write('id', 'data'));
|
||||
}
|
||||
|
||||
public function testDestroySession()
|
||||
{
|
||||
$this->memcached->expects($this->once())
|
||||
$this->memcached
|
||||
->expects($this->once())
|
||||
->method('delete')
|
||||
->will($this->returnValue(true));
|
||||
->with(self::PREFIX.'id')
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
|
||||
$this->assertTrue($this->storage->destroy(''));
|
||||
$this->assertTrue($this->storage->destroy('id'));
|
||||
}
|
||||
|
||||
public function testGcSession()
|
||||
@ -79,5 +94,26 @@ class MemcacheddSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($this->storage->gc(123));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getOptionFixtures
|
||||
*/
|
||||
public function testSupportedOptions($options, $supported)
|
||||
{
|
||||
try {
|
||||
new MemcachedSessionHandler($this->memcached, $options);
|
||||
$this->assertTrue($supported);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertFalse($supported);
|
||||
}
|
||||
}
|
||||
|
||||
public function getOptionFixtures()
|
||||
{
|
||||
return array(
|
||||
array(array('prefix' => 'session'), true),
|
||||
array(array('expiretime' => 100), true),
|
||||
array(array('prefix' => 'session', 'expiretime' => 200), true),
|
||||
array(array('expiretime' => 100, 'foo' => 'bar'), false),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user