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

Diff for: README.md

+15-1
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

Diff for: RedisCachePool.php

+50-3
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
/**

Diff for: Tests/ArrayIntegrationHierarchyTest.php

+19
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+
}

Diff for: Tests/ArrayIntegrationPoolTest.php

+19
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+
}

Diff for: Tests/ArrayIntegrationSimpleCacheTest.php

+19
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+
}

Diff for: Tests/ArrayIntegrationTagTest.php

+19
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+
}

Diff for: Tests/ClusterIntegrationHierarchyTest.php

+19
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+
}

Diff for: Tests/ClusterIntegrationPoolTest.php

+19
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+
}

Diff for: Tests/ClusterIntegrationSimpleCacheTest.php

+19
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+
}

Diff for: Tests/ClusterIntegrationTagTest.php

+19
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+
}

Diff for: Tests/CreateRedisArrayCachePoolTrait.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Adapter\Redis\RedisCachePool;
15+
16+
trait CreateRedisArrayCachePoolTrait
17+
{
18+
private $client = null;
19+
20+
public function createCachePool()
21+
{
22+
return new RedisCachePool($this->getClient());
23+
}
24+
25+
private function getClient()
26+
{
27+
if ($this->client === null) {
28+
$this->client = new \RedisArray(['127.0.0.1:6379']);
29+
}
30+
31+
return $this->client;
32+
}
33+
34+
public function createSimpleCache()
35+
{
36+
return $this->createCachePool();
37+
}
38+
}

Diff for: Tests/CreateRedisClusterPoolTrait.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Adapter\Redis\RedisCachePool;
15+
16+
trait CreateRedisClusterPoolTrait
17+
{
18+
private $client = null;
19+
20+
public function createCachePool()
21+
{
22+
return new RedisCachePool($this->getClient());
23+
}
24+
25+
private function getClient()
26+
{
27+
if ($this->client === null) {
28+
$this->client = new \RedisCluster(null, ['127.0.0.1:7000', '127.0.0.1:7001', '127.0.0.1:7002']);
29+
}
30+
31+
return $this->client;
32+
}
33+
34+
public function createSimpleCache()
35+
{
36+
return $this->createCachePool();
37+
}
38+
}

Diff for: Tests/CreatePoolTrait.php renamed to Tests/CreateRedisPoolTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Cache\Adapter\Redis\RedisCachePool;
1515

16-
trait CreatePoolTrait
16+
trait CreateRedisPoolTrait
1717
{
1818
private $client = null;
1919

Diff for: Tests/IntegrationHierarchyTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
class IntegrationHierarchyTest extends HierarchicalCachePoolTest
1717
{
18-
use CreatePoolTrait;
18+
use CreateRedisPoolTrait;
1919
}

Diff for: Tests/IntegrationPoolTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
class IntegrationPoolTest extends BaseTest
1717
{
18-
use CreatePoolTrait;
18+
use CreateRedisPoolTrait;
1919
}

Diff for: Tests/IntegrationSimpleCacheTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
class IntegrationSimpleCacheTest extends BaseTest
1717
{
18-
use CreatePoolTrait;
18+
use CreateRedisPoolTrait;
1919
}

Diff for: Tests/IntegrationTagTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
class IntegrationTagTest extends TaggableCachePoolTest
1717
{
18-
use CreatePoolTrait;
18+
use CreateRedisPoolTrait;
1919
}

0 commit comments

Comments
 (0)