1212namespace Cache \Adapter \Redis ;
1313
1414use Cache \Adapter \Common \AbstractCachePool ;
15+ use Cache \Adapter \Common \Exception \CachePoolException ;
1516use Cache \Adapter \Common \PhpCacheItem ;
1617use Cache \Hierarchy \HierarchicalCachePoolTrait ;
1718use 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 /**
0 commit comments