@@ -38,6 +38,7 @@ trait RedisTrait
3838 'tcp_keepalive ' => 0 ,
3939 'lazy ' => null ,
4040 'redis_cluster ' => false ,
41+ 'redis_sentinel ' => null ,
4142 'dbindex ' => 0 ,
4243 'failover ' => 'none ' ,
4344 ];
@@ -146,9 +147,13 @@ public static function createConnection($dsn, array $options = [])
146147 throw new InvalidArgumentException (sprintf ('Invalid Redis DSN: %s ' , $ dsn ));
147148 }
148149
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+
149154 $ params += $ query + $ options + self ::$ defaultConnectionOptions ;
150155
151- if (null === $ params ['class ' ] && \extension_loaded ('redis ' )) {
156+ if (null === $ params ['class ' ] && ! isset ( $ params [ ' redis_sentinel ' ]) && \extension_loaded ('redis ' )) {
152157 $ class = $ params ['redis_cluster ' ] ? \RedisCluster::class : (1 < \count ($ hosts ) ? \RedisArray::class : \Redis::class);
153158 } else {
154159 $ class = null === $ params ['class ' ] ? \Predis \Client::class : $ params ['class ' ];
@@ -246,6 +251,12 @@ public static function createConnection($dsn, array $options = [])
246251 } elseif (is_a ($ class , \Predis \Client::class, true )) {
247252 if ($ params ['redis_cluster ' ]) {
248253 $ 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 ' ];
249260 }
250261 $ params += ['parameters ' => []];
251262 $ params ['parameters ' ] += [
@@ -268,6 +279,9 @@ public static function createConnection($dsn, array $options = [])
268279 }
269280
270281 $ redis = new $ class ($ hosts , array_diff_key ($ params , self ::$ defaultConnectionOptions ));
282+ if (isset ($ params ['redis_sentinel ' ])) {
283+ $ redis ->getConnection ()->setSentinelTimeout ($ params ['timeout ' ]);
284+ }
271285 } elseif (class_exists ($ class , false )) {
272286 throw new InvalidArgumentException (sprintf ('"%s" is not a subclass of "Redis", "RedisArray", "RedisCluster" nor "Predis\Client". ' , $ class ));
273287 } else {
0 commit comments