-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
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
commit 9630dbbf2c900ec08eb8a6082d79195b7a7626c0
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...ymfony/Component/HttpFoundation/Tests/Session/Storage/Handler/RedisSessionHandlerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
|
||
|
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a typo in the method name. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?