12
12
namespace Symfony \Component \Cache \Traits ;
13
13
14
14
use Predis \Connection \Aggregate \ClusterInterface ;
15
- use Predis \Connection \Aggregate \PredisCluster ;
16
15
use Predis \Connection \Aggregate \RedisCluster ;
17
16
use Predis \Connection \Factory ;
18
17
use Predis \Response \Status ;
@@ -48,9 +47,7 @@ private function init($redisClient, $namespace = '', $defaultLifetime = 0)
48
47
if (preg_match ('#[^-+_.A-Za-z0-9]# ' , $ namespace , $ match )) {
49
48
throw new InvalidArgumentException (sprintf ('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed. ' , $ match [0 ]));
50
49
}
51
- if ($ redisClient instanceof \RedisCluster) {
52
- $ this ->enableVersioning ();
53
- } elseif (!$ redisClient instanceof \Redis && !$ redisClient instanceof \RedisArray && !$ redisClient instanceof \Predis \Client && !$ redisClient instanceof RedisProxy) {
50
+ if (!$ redisClient instanceof \Redis && !$ redisClient instanceof \RedisArray && !$ redisClient instanceof \RedisCluster && !$ redisClient instanceof \Predis \Client && !$ redisClient instanceof RedisProxy) {
54
51
throw new InvalidArgumentException (sprintf ('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given ' , __METHOD__ , \is_object ($ redisClient ) ? \get_class ($ redisClient ) : \gettype ($ redisClient )));
55
52
}
56
53
$ this ->redis = $ redisClient ;
@@ -207,9 +204,6 @@ protected function doHave($id)
207
204
*/
208
205
protected function doClear ($ namespace )
209
206
{
210
- // When using a native Redis cluster, clearing the cache is done by versioning in AbstractTrait::clear().
211
- // This means old keys are not really removed until they expire and may need garbage collection.
212
-
213
207
$ cleared = true ;
214
208
$ hosts = [$ this ->redis ];
215
209
$ evalArgs = [[$ namespace ], 0 ];
@@ -218,21 +212,23 @@ protected function doClear($namespace)
218
212
$ evalArgs = [0 , $ namespace ];
219
213
220
214
$ connection = $ this ->redis ->getConnection ();
221
- if ($ connection instanceof PredisCluster ) {
215
+ if ($ connection instanceof ClusterInterface && $ connection instanceof \Traversable ) {
222
216
$ hosts = [];
223
217
foreach ($ connection as $ c ) {
224
218
$ hosts [] = new \Predis \Client ($ c );
225
219
}
226
- } elseif ($ connection instanceof RedisCluster) {
227
- return false ;
228
220
}
229
221
} elseif ($ this ->redis instanceof \RedisArray) {
230
222
$ hosts = [];
231
223
foreach ($ this ->redis ->_hosts () as $ host ) {
232
224
$ hosts [] = $ this ->redis ->_instance ($ host );
233
225
}
234
226
} elseif ($ this ->redis instanceof \RedisCluster) {
235
- return false ;
227
+ $ hosts = [];
228
+ foreach ($ this ->redis ->_masters () as $ host ) {
229
+ $ hosts [] = $ h = new \Redis ();
230
+ $ h ->connect ($ host [0 ], $ host [1 ]);
231
+ }
236
232
}
237
233
foreach ($ hosts as $ host ) {
238
234
if (!isset ($ namespace [0 ])) {
@@ -259,7 +255,7 @@ protected function doClear($namespace)
259
255
$ keys = $ keys [1 ];
260
256
}
261
257
if ($ keys ) {
262
- $ host -> del ($ keys );
258
+ $ this -> doDelete ($ keys );
263
259
}
264
260
} while ($ cursor = (int ) $ cursor );
265
261
}
@@ -331,7 +327,16 @@ private function pipeline(\Closure $generator)
331
327
{
332
328
$ ids = [];
333
329
334
- if ($ this ->redis instanceof \Predis \Client && !$ this ->redis ->getConnection () instanceof ClusterInterface) {
330
+ if ($ this ->redis instanceof \RedisCluster || ($ this ->redis instanceof \Predis \Client && $ this ->redis ->getConnection () instanceof RedisCluster)) {
331
+ // phpredis & predis don't support pipelining with RedisCluster
332
+ // see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining
333
+ // see https://github.com/nrk/predis/issues/267#issuecomment-123781423
334
+ $ results = [];
335
+ foreach ($ generator () as $ command => $ args ) {
336
+ $ results [] = \call_user_func_array ([$ this ->redis , $ command ], $ args );
337
+ $ ids [] = $ args [0 ];
338
+ }
339
+ } elseif ($ this ->redis instanceof \Predis \Client) {
335
340
$ results = $ this ->redis ->pipeline (function ($ redis ) use ($ generator , &$ ids ) {
336
341
foreach ($ generator () as $ command => $ args ) {
337
342
\call_user_func_array ([$ redis , $ command ], $ args );
@@ -355,15 +360,6 @@ private function pipeline(\Closure $generator)
355
360
foreach ($ results as $ k => list ($ h , $ c )) {
356
361
$ results [$ k ] = $ connections [$ h ][$ c ];
357
362
}
358
- } elseif ($ this ->redis instanceof \RedisCluster || ($ this ->redis instanceof \Predis \Client && $ this ->redis ->getConnection () instanceof ClusterInterface)) {
359
- // phpredis & predis don't support pipelining with RedisCluster
360
- // see https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#pipelining
361
- // see https://github.com/nrk/predis/issues/267#issuecomment-123781423
362
- $ results = [];
363
- foreach ($ generator () as $ command => $ args ) {
364
- $ results [] = \call_user_func_array ([$ this ->redis , $ command ], $ args );
365
- $ ids [] = $ args [0 ];
366
- }
367
363
} else {
368
364
$ this ->redis ->multi (\Redis::PIPELINE );
369
365
foreach ($ generator () as $ command => $ args ) {
0 commit comments