feature #18172 [Cache] Redis adapter (gcds, nicolas-grekas)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Cache] Redis adapter

| Q             | A
| ------------- | ---
| Branch        | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17441
| License       | MIT
| Doc PR        | -

Commits
-------

6b7a1fc [Cache] Finish Redis adapter
4893cbc Added RedisAdapter
This commit is contained in:
Fabien Potencier 2016-03-15 17:19:57 +01:00
commit ded8491282
3 changed files with 164 additions and 1 deletions

View File

@ -31,7 +31,9 @@ cache:
- .phpunit
- php-$MIN_PHP
services: mongodb
services:
- mongodb
- redis-server
before_install:
# Matrix lines for intermediate PHP versions are skipped for pull requests
@ -48,6 +50,7 @@ before_install:
- if [[ $TRAVIS_PHP_VERSION = 5.* && ! $deps ]]; then (cd src/Symfony/Component/Debug/Resources/ext && phpize && ./configure && make && echo extension = $(pwd)/modules/symfony_debug.so >> $INI_FILE); fi;
- if [[ $TRAVIS_PHP_VERSION = 5.* ]]; then pecl install -f memcached-2.1.0; fi;
- if [[ $TRAVIS_PHP_VERSION != hhvm ]]; then echo extension = ldap.so >> $INI_FILE; fi;
- if [[ $TRAVIS_PHP_VERSION != hhvm ]]; then echo extension = redis.so >> $INI_FILE; fi;
- if [[ $TRAVIS_PHP_VERSION != hhvm ]]; then phpenv config-rm xdebug.ini; fi;
- if [[ $deps != skip ]]; then composer self-update; fi;
- if [[ $deps != skip && $TRAVIS_REPO_SLUG = symfony/symfony ]]; then cp .composer/* ~/.composer/; composer global install --prefer-dist; fi;

View File

@ -0,0 +1,115 @@
<?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\Cache\Adapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
/**
* @author Aurimas Niekis <aurimas@niekis.lt>
*/
class RedisAdapter extends AbstractAdapter
{
private $redis;
public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0)
{
$this->redis = $redisConnection;
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
}
parent::__construct($namespace, $defaultLifetime);
}
/**
* {@inheritdoc}
*/
protected function doFetch(array $ids)
{
$values = $this->redis->mget($ids);
$index = 0;
$result = [];
foreach ($ids as $id) {
if (false !== $value = $values[$index++]) {
$result[$id] = unserialize($value);
}
}
return $result;
}
/**
* {@inheritdoc}
*/
protected function doHave($id)
{
return $this->redis->exists($id);
}
/**
* {@inheritdoc}
*/
protected function doClear($namespace)
{
if (!isset($namespace[0])) {
$this->redis->flushDB();
} else {
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
// can hang your server when it is executed against large databases (millions of items).
// Whenever you hit this scale, it is advised to deploy one Redis database per cache pool
// instead of using namespaces, so that the above FLUSHDB is used instead.
$this->redis->eval(sprintf("local keys=redis.call('KEYS','%s*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end", $namespace));
}
return true;
}
/**
* {@inheritdoc}
*/
protected function doDelete(array $ids)
{
$this->redis->del($ids);
return true;
}
/**
* {@inheritdoc}
*/
protected function doSave(array $values, $lifetime)
{
$failed = array();
foreach ($values as $id => $v) {
try {
$values[$id] = serialize($v);
} catch (\Exception $e) {
$failed[] = $id;
}
}
if (!$this->redis->mSet($values)) {
return false;
}
if ($lifetime >= 1) {
foreach ($values as $id => $v) {
$this->redis->expire($id, $lifetime);
}
}
return $failed;
}
}

View File

@ -0,0 +1,45 @@
<?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\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\RedisAdapter;
/**
* @requires extension redis
*/
class RedisAdapterTest extends CachePoolTest
{
private static $redis;
public function createCachePool()
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__));
}
public static function setupBeforeClass()
{
self::$redis = new \Redis();
self::$redis->connect('127.0.0.1');
self::$redis->select(1993);
}
public static function tearDownAfterClass()
{
self::$redis->flushDB();
self::$redis->close();
}
}