Skip to content

[Cache] Added RedisAdapter #17441

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ cache:
- .phpunit
- php-$MIN_PHP

services: mongodb
services:
- mongodb
- redis-server

before_install:
- if [[ ! $deps && ! $TRAVIS_PHP_VERSION = ${MIN_PHP%.*} && $TRAVIS_PHP_VERSION != hhvm && $TRAVIS_PULL_REQUEST != false ]]; then deps=skip; fi;
Expand Down
107 changes: 107 additions & 0 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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;

/**
* @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;

parent::__construct($namespace, $defaultLifetime);
}

/**
* {@inheritdoc}
*/
protected function doFetch(array $ids)
{
$values = $this->redis->mget($ids);

$index = 0;
$result = [];

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

if (false === $value) {
continue;
}

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

return $result;
}

/**
* {@inheritdoc}
*/
protected function doHave($id)
{
return $this->redis->exists($id);
}

/**
* {@inheritdoc}
*/
protected function doClear()
{
return $this->redis->flushDB();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this consider the namespace? I mean like this it would flush all keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Redis does not support delete with prefix or etc... I could do get keys and when do foreach but you know...

P.S. APCU does also clear all cache...

}

/**
* {@inheritdoc}
*/
protected function doDelete(array $ids)
{
$this->redis->del($ids);

return true;
}

/**
* {@inheritdoc}
*/
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);
}

if (false === $response) {
$failed[] = $key;
}
}

return count($failed) > 0 ? $failed : true;
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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;

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

public function createCachePool()
{
return new RedisAdapter($this->getRedis(), __CLASS__);
}

private function getRedis()
{
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()
{
self::$redis->flushDB();
self::$redis->close();
}
}