Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4893cbc

Browse files
Aurimas Niekisnicolas-grekas
Aurimas Niekis
authored andcommitted
Added RedisAdapter
1 parent b19ce5e commit 4893cbc

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ cache:
3131
- .phpunit
3232
- php-$MIN_PHP
3333

34-
services: mongodb
34+
services:
35+
- mongodb
36+
- redis-server
3537

3638
before_install:
3739
# Matrix lines for intermediate PHP versions are skipped for pull requests
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Adapter;
13+
14+
/**
15+
* @author Aurimas Niekis <[email protected]>
16+
*/
17+
class RedisAdapter extends AbstractAdapter
18+
{
19+
/**
20+
* @var \Redis
21+
*/
22+
private $redis;
23+
24+
/**
25+
* @param \Redis $redisConnection
26+
* @param string $namespace
27+
* @param int $defaultLifetime
28+
*/
29+
public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0)
30+
{
31+
$this->redis = $redisConnection;
32+
33+
parent::__construct($namespace, $defaultLifetime);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected function doFetch(array $ids)
40+
{
41+
$values = $this->redis->mget($ids);
42+
43+
$index = 0;
44+
$result = [];
45+
46+
foreach ($ids as $id) {
47+
$value = $values[$index++];
48+
49+
if (false === $value) {
50+
continue;
51+
}
52+
53+
$result[$id] = unserialize($value);
54+
}
55+
56+
return $result;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
protected function doHave($id)
63+
{
64+
return $this->redis->exists($id);
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
protected function doClear()
71+
{
72+
return $this->redis->flushDB();
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
protected function doDelete(array $ids)
79+
{
80+
$this->redis->del($ids);
81+
82+
return true;
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
protected function doSave(array $values, $lifetime)
89+
{
90+
$failed = [];
91+
foreach ($values as $key => $value) {
92+
$value = serialize($value);
93+
94+
if ($lifetime < 1) {
95+
$response = $this->redis->set($key, $value);
96+
} else {
97+
$response = $this->redis->setex($key, $lifetime, $value);
98+
}
99+
100+
if (false === $response) {
101+
$failed[] = $key;
102+
}
103+
}
104+
105+
return count($failed) > 0 ? $failed : true;
106+
}
107+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Cache\IntegrationTests\CachePoolTest;
15+
use Symfony\Component\Cache\Adapter\RedisAdapter;
16+
17+
class RedisAdapterTest extends CachePoolTest
18+
{
19+
/**
20+
* @var \Redis
21+
*/
22+
private static $redis;
23+
24+
public function createCachePool()
25+
{
26+
return new RedisAdapter($this->getRedis(), __CLASS__);
27+
}
28+
29+
private function getRedis()
30+
{
31+
if (self::$redis) {
32+
return self::$redis;
33+
}
34+
35+
self::$redis = new \Redis();
36+
self::$redis->connect('127.0.0.1');
37+
self::$redis->select(1993);
38+
39+
return self::$redis;
40+
}
41+
42+
public static function tearDownAfterClass()
43+
{
44+
self::$redis->flushDB();
45+
self::$redis->close();
46+
}
47+
}

0 commit comments

Comments
 (0)