Skip to content

Commit 33c6494

Browse files
wshaferprisis
authored andcommitted
Allow \RedisArray and \RedisCluster objects for RedisCachePool (#194)
1 parent 2fd4b97 commit 33c6494

18 files changed

+326
-9
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,28 @@ composer require cache/redis-adapter
2020

2121
### Use
2222

23-
To create an instance of `RedisCachePool` you need to configure a `\Redis` client.
23+
To create an instance of `RedisCachePool` you need to configure a `\Redis`, `\RedisArray` or `\RedisCluster` client.
2424

25+
\Redis
2526
```php
2627
$client = new \Redis();
2728
$client->connect('127.0.0.1', 6379);
2829
$pool = new RedisCachePool($client);
2930
```
3031

32+
\RedisArray
33+
```php
34+
$client = new \RedisArray(['127.0.0.1:6379', '127.0.0.2:6379']);
35+
$pool = new RedisCachePool($client);
36+
```
37+
38+
\RedisCluster
39+
```php
40+
$client = new \RedisCluster(null, ['127.0.0.1:7000', '127.0.0.2:7001', '127.0.0.2:7002',]);
41+
$pool = new RedisCachePool($client);
42+
```
43+
44+
_See [PhpRedis](https://github.com/phpredis/phpredis) for more connection options_
3145

3246
### Contribute
3347

RedisCachePool.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\Adapter\Redis;
1313

1414
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Adapter\Common\Exception\CachePoolException;
1516
use Cache\Adapter\Common\PhpCacheItem;
1617
use Cache\Hierarchy\HierarchicalCachePoolTrait;
1718
use Cache\Hierarchy\HierarchicalPoolInterface;
@@ -29,10 +30,19 @@ class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterf
2930
protected $cache;
3031

3132
/**
32-
* @param \Redis $cache
33+
* @param \Redis|\RedisArray|\RedisCluster $cache
3334
*/
34-
public function __construct(\Redis $cache)
35+
public function __construct($cache)
3536
{
37+
if (!$cache instanceof \Redis
38+
&& !$cache instanceof \RedisArray
39+
&& !$cache instanceof \RedisCluster
40+
) {
41+
throw new CachePoolException(
42+
'Cache instance must be of type \Redis, \RedisArray, or \RedisCluster'
43+
);
44+
}
45+
3646
$this->cache = $cache;
3747
}
3848

@@ -53,7 +63,44 @@ protected function fetchObjectFromCache($key)
5363
*/
5464
protected function clearAllObjectsFromCache()
5565
{
56-
return $this->cache->flushDb();
66+
if ($this->cache instanceof \RedisCluster) {
67+
return $this->clearAllObjectsFromCacheCluster();
68+
}
69+
70+
$result = $this->cache->flushDb();
71+
72+
if (!is_array($result)) {
73+
return $result;
74+
}
75+
76+
$success = true;
77+
78+
foreach ($result as $serverResult) {
79+
if (!$serverResult) {
80+
$success = false;
81+
break;
82+
}
83+
}
84+
85+
return $success;
86+
}
87+
88+
/**
89+
* Clear all objects from all nodes in the cluster.
90+
*
91+
* @return bool false if error
92+
*/
93+
protected function clearAllObjectsFromCacheCluster()
94+
{
95+
$nodes = $this->cache->_masters();
96+
97+
foreach ($nodes as $node) {
98+
if (!$this->cache->flushDB($node)) {
99+
return false;
100+
}
101+
}
102+
103+
return true;
57104
}
58105

59106
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\HierarchicalCachePoolTest;
15+
16+
class ArrayIntegrationHierarchyTest extends HierarchicalCachePoolTest
17+
{
18+
use CreateRedisArrayCachePoolTrait;
19+
}

Tests/ArrayIntegrationPoolTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\CachePoolTest as BaseTest;
15+
16+
class ArrayIntegrationPoolTest extends BaseTest
17+
{
18+
use CreateRedisArrayCachePoolTrait;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\SimpleCacheTest as BaseTest;
15+
16+
class ArrayIntegrationSimpleCacheTest extends BaseTest
17+
{
18+
use CreateRedisArrayCachePoolTrait;
19+
}

Tests/ArrayIntegrationTagTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\TaggableCachePoolTest;
15+
16+
class ArrayIntegrationTagTest extends TaggableCachePoolTest
17+
{
18+
use CreateRedisArrayCachePoolTrait;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\HierarchicalCachePoolTest;
15+
16+
class ClusterIntegrationHierarchyTest extends HierarchicalCachePoolTest
17+
{
18+
use CreateRedisClusterPoolTrait;
19+
}

Tests/ClusterIntegrationPoolTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\CachePoolTest as BaseTest;
15+
16+
class ClusterIntegrationPoolTest extends BaseTest
17+
{
18+
use CreateRedisClusterPoolTrait;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\SimpleCacheTest as BaseTest;
15+
16+
class ClusterIntegrationSimpleCacheTest extends BaseTest
17+
{
18+
use CreateRedisClusterPoolTrait;
19+
}

Tests/ClusterIntegrationTagTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Redis\Tests;
13+
14+
use Cache\IntegrationTests\TaggableCachePoolTest;
15+
16+
class ClusterIntegrationTagTest extends TaggableCachePoolTest
17+
{
18+
use CreateRedisClusterPoolTrait;
19+
}

0 commit comments

Comments
 (0)