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
Next Next commit
Added RedisAdapter
  • Loading branch information
Aurimas Niekis authored and nicolas-grekas committed Mar 15, 2016
commit 4893cbc24a863b322675abc2a71852f238c5057f
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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();
}

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