merged branch pulzarraider/redis_session_storage (PR #3498)

Commits
-------

c4ee947 Native Redis Session Storage update
665f593 NativeRedisSessionStorage added

Discussion
----------

[HttpFoundation] Native Redis Session Storage

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

---------------------------------------------------------------------------

by lstrojny at 2012-03-04T23:15:43Z

Does Symfony (or any of its dependencies) has Redis support in any form whatsoever? If not this might be a good point to decide which clients to support

---------------------------------------------------------------------------

by lsmith77 at 2012-03-04T23:36:11Z

well ideally we just get this cache interface stuff done .. for this use case it would be perfect.

---------------------------------------------------------------------------

by pulzarraider at 2012-03-05T00:35:59Z

There is RedisProfilerStorage available (based on phpredis). I prefer and write code for [phpredis](https://github.com/nicolasff/phpredis).

It's recommended by [official Redis homepage](http://redis.io/clients#PHP). [In this benchmark](http://dev.af83.com/2011/01/01/which-php-library-to-use-with-redis-the-benchmark.html
) is fastest and less memory consumpting.

But if somebody prefer predis (with phpiredis), rediska or something other widely used, there are no limitations to add support of it to Symfony.

My opinion is, that the C extension should be supported at first, because of good performance and native session storage support. Redis is quite young and the process of creating PHP clients is comparable to Memcache.
There were created pure PHP Memcache clients in the past (Google found for example [this](http://www.phpclasses.org/browse/file/20284.html) and [this](http://code.blitzaffe.com/pages/phpclasses/files/memcached_client_52-12)), but they are not being used now. Everyone, who is seriously thinking about performance, is using only the C Redis/Memcache(d)/... extensions.

---------------------------------------------------------------------------

by drak at 2012-03-05T07:40:06Z

+1 on this PR.  Needs a test written though.
I don't think there is any need to wait for #3493 imo.  I'll deal with it if this is merged before #3493.
Are there any PHP ini settings for this for this driver or is everything via the `session.save_path` directive? (A quick look at the C code seems to indicate there are no explicit ini directives).

---------------------------------------------------------------------------

by lstrojny at 2012-03-05T12:14:34Z

@pulzarraider I don’t necessarily disagree with the usage of phpredis, I just wanted to bring up the issues of various clients and people having different preferences about them.

---------------------------------------------------------------------------

by fabpot at 2012-03-05T14:46:22Z

@pulzarraider Can you add some unit tests before I merge?

---------------------------------------------------------------------------

by pulzarraider at 2012-03-11T20:19:57Z

@drak No there are no php.ini settings. Only RedisArray has some, but it's another feature.

@fabpot I've added simple test based on other session storage tests.

I planned to create a RedisSessionStorage, too, but I have no time for it now. This can be added later in another PR as it's independent from NativeRedisSessionStorage.

---------------------------------------------------------------------------

by drak at 2012-03-12T02:21:25Z

The code looks OK to me.

---------------------------------------------------------------------------

by fabpot at 2012-03-15T06:05:27Z

#3493 has been merged now.

---------------------------------------------------------------------------

by pulzarraider at 2012-03-16T23:21:27Z

Code updated.
This commit is contained in:
Fabien Potencier 2012-03-23 13:42:58 +01:00
commit 14a83ce064
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?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;
/**
* NativeRedisSessionStorage.
*
* Driver for the redis session save hadlers provided by the redis PHP extension.
*
* @see https://github.com/nicolasff/phpredis
*
* @author Andrej Hudec <pulzarraider@gmail.com>
*/
class NativeRedisSessionHandler extends NativeSessionHandler
{
/**
* Constructor.
*
* @param string $savePath Path of redis server.
*/
public function __construct($savePath = 'tcp://127.0.0.1:6379?persistent=0')
{
if (!extension_loaded('redis')) {
throw new \RuntimeException('PHP does not have "redis" session module registered');
}
if (null === $savePath) {
$savePath = ini_get('session.save_path');
}
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', $savePath);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Symfony\Tests\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeRedisSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
/**
* Test class for NativeRedisSessionHandlerTest.
*
* @runTestsInSeparateProcesses
*/
class NativeRedisSessionHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testSaveHandlers()
{
if (!extension_loaded('redis')) {
$this->markTestSkipped('Skipped tests Redis extension is not present');
}
$storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeRedisSessionHandler('tcp://127.0.0.1:6379?persistent=0'));
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->assertEquals('redis', $storage->getSaveHandler()->getSaveHandlerName());
$this->assertEquals('redis', ini_get('session.save_handler'));
} else {
$this->assertEquals('redis', $storage->getSaveHandler()->getSaveHandlerName());
$this->assertEquals('user', ini_get('session.save_handler'));
}
$this->assertEquals('tcp://127.0.0.1:6379?persistent=0', ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
}
}