12
12
namespace Cache \Adapter \Redis ;
13
13
14
14
use Cache \Adapter \Common \AbstractCachePool ;
15
+ use Cache \Adapter \Common \Exception \CachePoolException ;
15
16
use Cache \Adapter \Common \PhpCacheItem ;
16
17
use Cache \Hierarchy \HierarchicalCachePoolTrait ;
17
18
use Cache \Hierarchy \HierarchicalPoolInterface ;
@@ -29,10 +30,19 @@ class RedisCachePool extends AbstractCachePool implements HierarchicalPoolInterf
29
30
protected $ cache ;
30
31
31
32
/**
32
- * @param \Redis $cache
33
+ * @param \Redis|\RedisArray|\RedisCluster $cache
33
34
*/
34
- public function __construct (\ Redis $ cache )
35
+ public function __construct ($ cache )
35
36
{
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
+
36
46
$ this ->cache = $ cache ;
37
47
}
38
48
@@ -53,7 +63,44 @@ protected function fetchObjectFromCache($key)
53
63
*/
54
64
protected function clearAllObjectsFromCache ()
55
65
{
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 ;
57
104
}
58
105
59
106
/**
0 commit comments