8000 [Cache] Redis adapter by nicolas-grekas · Pull Request #18172 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Redis adapter #18172

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

Merged
merged 2 commits into from
Mar 15, 2016
Merged
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
Prev Previous commit
[Cache] Finish Redis adapter
  • Loading branch information
nicolas-grekas committed Mar 15, 2016
commit 6b7a1fcefadb4a343cb3551b93ce8b1d9e236df0
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,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;
Expand Down
64 changes: 36 additions & 28 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@

namespace Symfony\Component\Cache\Adapter;

use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
* @author Aurimas Niekis <aurimas@niekis.lt>
*/
class RedisAdapter extends AbstractAdapter
{
/**
* @var \Redis
*/
private $redis;

/**
* @param \Redis $redisConnection
* @param string $namespace
* @param int $defaultLifetime
*/
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);
}

Expand All @@ -39,18 +37,13 @@ public function __construct(\Redis $redisConnection, $namespace = '', $defaultLi
protected function doFetch(array $ids)
{
$values = $this->redis->mget($ids);

$index = 0;
$result = [];

foreach ($ids as $id) {
$value = $values[$index++];

if (false === $value) {
continue;
if (false !== $value = $values[$index++]) {
$result[$id] = unserialize($value);
}

$result[$id] = unserialize($value);
}

return $result;
Expand All @@ -67,9 +60,19 @@ protected function doHave($id)
/**
* {@inheritdoc}
*/
protected function doClear()
protected function doClear($namespace)
{
return $this->redis->flushDB();
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;
}

/**
Expand All @@ -87,21 +90,26 @@ protected function doDelete(array $ids)
*/
protected function doSave(array $values, $lifetime)
{
$failed = [];
foreach ($values as $key => $value) {
$value = serialize($value);

if ($lifetime < 1) {
$response = $this->redis->set($key, $value);
} else {
$response = $this->redis->setex($key, $lifetime, $value);
$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 (false === $response) {
$failed[] = $key;
if ($lifetime >= 1) {
foreach ($values as $id => $v) {
$this->redis->expire($id, $lifetime);
}
}

return count($failed) > 0 ? $failed : true;
return $failed;
}
}
20 changes: 9 additions & 11 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,27 @@
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\RedisAdapter;

/**
* @requires extension redis
*/
class RedisAdapterTest extends CachePoolTest
{
/**
* @var \Redis
*/
private static $redis;

public function createCachePool()
{
return new RedisAdapter($this->getRedis(), __CLASS__);
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}

return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__));
}

private function getRedis()
public static function setupBeforeClass()
{
if (self::$redis) {
return self::$redis;
}

self::$redis = new \Redis();
self::$redis->connect('127.0.0.1');
self::$redis->select(1993);

return self::$redis;
}

public static function tearDownAfterClass()
Expand Down
0