@@ -38,6 +38,7 @@ trait RedisTrait
38
38
'tcp_keepalive ' => 0 ,
39
39
'lazy ' => null ,
40
40
'redis_cluster ' => false ,
41
+ 'redis_sentinel ' => null ,
41
42
'dbindex ' => 0 ,
42
43
'failover ' => 'none ' ,
43
44
];
@@ -146,9 +147,13 @@ public static function createConnection($dsn, array $options = [])
146
147
throw new InvalidArgumentException (sprintf ('Invalid Redis DSN: %s ' , $ dsn ));
147
148
}
148
149
150
+ if (isset ($ params ['redis_sentinel ' ]) && !class_exists (\Predis \Client::class)) {
151
+ throw new CacheException (sprintf ('Redis Sentinel support requires the "predis/predis" package: %s ' , $ dsn ));
152
+ }
153
+
149
154
$ params += $ query + $ options + self ::$ defaultConnectionOptions ;
150
155
151
- if (null === $ params ['class ' ] && \extension_loaded ('redis ' )) {
156
+ if (null === $ params ['class ' ] && ! isset ( $ params [ ' redis_sentinel ' ]) && \extension_loaded ('redis ' )) {
152
157
$ class = $ params ['redis_cluster ' ] ? \RedisCluster::class : (1 < \count ($ hosts ) ? \RedisArray::class : \Redis::class);
153
158
} else {
154
159
$ class = null === $ params ['class ' ] ? \Predis \Client::class : $ params ['class ' ];
@@ -246,6 +251,12 @@ public static function createConnection($dsn, array $options = [])
246
251
} elseif (is_a ($ class , \Predis \Client::class, true )) {
247
252
if ($ params ['redis_cluster ' ]) {
248
253
$ params ['cluster ' ] = 'redis ' ;
254
+ if (isset ($ params ['redis_sentinel ' ])) {
255
+ throw new InvalidArgumentException (sprintf ('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: %s ' , $ dsn ));
256
+ }
257
+ } elseif (isset ($ params ['redis_sentinel ' ])) {
258
+ $ params ['replication ' ] = 'sentinel ' ;
259
+ $ params ['service ' ] = $ params ['redis_sentinel ' ];
249
260
}
250
261
$ params += ['parameters ' => []];
251
262
$ params ['parameters ' ] += [
@@ -268,6 +279,9 @@ public static function createConnection($dsn, array $options = [])
268
279
}
269
280
270
281
$ redis = new $ class ($ hosts , array_diff_key ($ params , self ::$ defaultConnectionOptions ));
282
+ if (isset ($ params ['redis_sentinel ' ])) {
283
+ $ redis ->getConnection ()->setSentinelTimeout ($ params ['timeout ' ]);
284
+ }
271
285
} elseif (class_exists ($ class , false )) {
272
286
throw new InvalidArgumentException (sprintf ('"%s" is not a subclass of "Redis", "RedisArray", "RedisCluster" nor "Predis\Client". ' , $ class ));
273
287
} else {
0 commit comments