8000 [2.2] [HttpFoundation] Add redis session storage by Baachi · Pull Request #4538 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.2] [HttpFoundation] Add redis session storage #4538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add redis session storage
  • Loading branch information
Baachi committed Jun 9, 2012
commit 9630dbbf2c900ec08eb8a6082d79195b7a7626c0
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

/**
* RedisSessionHandler
*
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class RedisSessionHandler implements \SessionHandlerInterface
{
/**
* @var \Redis
*/
private $redis;

/**
* @var integer
*/
private $lifetime;

/**
* Constructor
*
* @param \Redis $redis The redis instance
* @param integer $lifetime Max lifetime in seconds to keep sessions stored.
*/
public function __construct(\Redis $redis, $lifetime)
{
$this->redis = $redis;
$this->lifetime = $lifetime;
}

/**
* {@inheritDoc}
*/
public function open($savePath, $sessionName)
{
return true;
}

/**
* {@inheritDoc}
*/
public function read($sessionId)
{
return (string) $this->redis->get($sessionId);
}

/**
* {@inheritDoc}
*/
public function write($sessionId, $data)
{
return $this->redis->setex($sessionId, $this->lifetime, $data);
}

/**
* {@inheritDoc}
*/
public function destroy($sessionId)
{
return 1 === $this->redis->delete($sessionId);
}

/**
* {@inheritDoc}
*/
public function gc($lifetime)
{
return true;
}

/**
* {@inheritDoc}
*/
public function close()
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/


namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;

/**
* RedisSessionHandlerTest
*
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class RedisSessionHandlerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RedisSessionHandler
*/
private $handler;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $redis;

public static function setUpBeforeClass()
{
if (!extension_loaded('redis')) {
self::markTestSkipped('The "redis" extension must be loaded');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you actually skip in beforeClass ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can skip the test in setUpBeforeClass.
Because, markTestSkipped is a static method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Baachi thanks for the tip I had never taken the time to look at the src code before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vicb No problem :)

Do you have other suggestions?

}
}


protected function setup()
{
$this->redis = $this->getMockBuilder('Redis')->disableOriginalConstructor()->getMock();
$this->handler = new RedisSessionHandler($this->redis, 86400);
}

public function testOpen()
{
$this->assertTrue($this->handler->open('test', 'test'));
}

public function testClose()
{
$this->assertTrue($this->handler->close());
}

public function testGc()
{
$this->assertTrue($this->handler->gc(200));
}

public function testWrite()
{
$data = array();

$this->redis->expects($this->once())
->method('setex')
->will($this->returnCallback(function($key, $lifetime, $value) use (&$data) {
$data[$key] = $value;
return true;
}));

$this->handler->write('sessionid', 'foobar');

$this->assertArrayHasKey('sessionid', $data);
$this->assertEquals($data['sessionid'], 'foobar');
}

public function testRead()
{
$data = array(
'foo' => 'bar',
);

$this->redis->expects($this->any())
->method('get')
->will($this->returnCallback(function($key) use (&$data) {
return isset($data[$key]) ? $data[$key] : false;
}));

$this->assertEquals('bar', $this->handler->read('foo'));
$this->assertEquals('', $this->handler->read('not-exist'));
}

public function testDestory()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the method name. Should be testDestroy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Is fixed now.

{
$data = array(
'foo' => 'bar'
);

$this->redis->expects($this->any())
->method('delete')
->will($this->returnCallback(function($key) use (&$data) {
if (!isset($data[$key])) {
return false;
}
unset($data[$key]);
return 1;
}));

$this->assertTrue($this->handler->destroy('foo'));
$this->assertArrayNotHasKey('foo', $data);

$this->assertFalse($this->handler->destroy('not-exist'));
}
}
0