@@ -46,9 +46,10 @@ trait RedisTrait
4646 'retry_interval ' => 0 ,
4747 'tcp_keepalive ' => 0 ,
4848 'lazy ' => null ,
49- 'redis_cluster ' => false ,
50- 'relay_cluster_context ' => [],
5149 'redis_sentinel ' => null ,
50+ 'cluster ' => false ,
51+ 'sentinel ' => null ,
52+ 'relay_cluster_context ' => [],
5253 'dbindex ' => 0 ,
5354 'failover ' => 'none ' ,
5455 'ssl ' => null , // see https://php.net/context.ssl
@@ -90,13 +91,13 @@ private function init(\Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predi
9091 */
9192 public static function createConnection (#[\SensitiveParameter] string $ dsn , array $ options = []): \Redis |\RedisArray |\RedisCluster |\Predis \ClientInterface |Relay |RelayCluster
9293 {
93- if ( str_starts_with ( $ dsn , ' redis: ' ) ) {
94- $ scheme = 'redis ' ;
95- } elseif ( str_starts_with ($ dsn , 'rediss: ' )) {
96- $ scheme = ' rediss ' ;
97- } else {
98- throw new InvalidArgumentException ('Invalid Redis DSN: it does not start with "redis[s]:". ' );
99- }
94+ $ scheme = match ( true ) {
95+ str_starts_with ( $ dsn , ' redis: ' ) => 'redis ' ,
96+ str_starts_with ($ dsn , 'rediss: ' ) => ' rediss ' ,
97+ str_starts_with ( $ dsn , ' valkey: ' ) => ' valkey ' ,
98+ str_starts_with ( $ dsn , ' valkeys: ' ) => ' valkeys ' ,
99+ default => throw new InvalidArgumentException ('Invalid Redis DSN: it does not start with "redis[s]:" nor "valkey[s]:" . ' ),
100+ };
100101
101102 if (!\extension_loaded ('redis ' ) && !class_exists (\Predis \Client::class)) {
102103 throw new CacheException ('Cannot find the "redis" extension nor the "predis/predis" package. ' );
@@ -124,7 +125,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
124125
125126 $ query = $ hosts = [];
126127
127- $ tls = 'rediss ' === $ scheme ;
128+ $ tls = 'rediss ' === $ scheme || ' valkeys ' === $ scheme ;
128129 $ tcpScheme = $ tls ? 'tls ' : 'tcp ' ;
129130
130131 if (isset ($ params ['query ' ])) {
@@ -177,32 +178,41 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
177178
178179 $ params += $ query + $ options + self ::$ defaultConnectionOptions ;
179180
180- if (isset ($ params ['redis_sentinel ' ]) && isset ($ params ['sentinel_master ' ])) {
181- throw new InvalidArgumentException ('Cannot use both "redis_sentinel" and "sentinel_master" at the same time. ' );
181+ $ aliases = [
182+ 'sentinel_master ' => 'sentinel ' ,
183+ 'redis_sentinel ' => 'sentinel ' ,
184+ 'redis_cluster ' => 'cluster ' ,
185+ ];
186+ foreach ($ aliases as $ alias => $ key ) {
187+ $ params [$ key ] = match (true ) {
188+ \array_key_exists ($ key , $ query ) => $ query [$ key ],
189+ \array_key_exists ($ alias , $ query ) => $ query [$ alias ],
190+ \array_key_exists ($ key , $ options ) => $ options [$ key ],
191+ \array_key_exists ($ alias , $ options ) => $ options [$ alias ],
192+ default => $ params [$ key ],
193+ };
182194 }
183195
184- $ params ['redis_sentinel ' ] ??= $ params ['sentinel_master ' ] ?? null ;
185-
186- if (isset ($ params ['redis_sentinel ' ]) && !class_exists (\Predis \Client::class) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
196+ if (isset ($ params ['sentinel ' ]) && !class_exists (\Predis \Client::class) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
187197 throw new CacheException ('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay". ' );
188198 }
189199
190200 if (isset ($ params ['lazy ' ])) {
191201 $ params ['lazy ' ] = filter_var ($ params ['lazy ' ], \FILTER_VALIDATE_BOOLEAN );
192202 }
203+ $ params ['cluster ' ] = filter_var ($ params ['cluster ' ], \FILTER_VALIDATE_BOOLEAN );
193204
194- $ params ['redis_cluster ' ] = filter_var ($ params ['redis_cluster ' ], \FILTER_VALIDATE_BOOLEAN );
195- if ($ params ['redis_cluster ' ] && isset ($ params ['redis_sentinel ' ])) {
196- throw new InvalidArgumentException ('Cannot use both "redis_cluster" and "redis_sentinel" at the same time. ' );
205+ if ($ params ['cluster ' ] && isset ($ params ['sentinel ' ])) {
206+ throw new InvalidArgumentException ('Cannot use both "cluster" and "sentinel" at the same time. ' );
197207 }
198208
199209 $ class = $ params ['class ' ] ?? match (true ) {
200- $ params ['redis_cluster ' ] => match (true ) {
210+ $ params ['cluster ' ] => match (true ) {
201211 \extension_loaded ('redis ' ) => \RedisCluster::class,
202212 \extension_loaded ('relay ' ) => RelayCluster::class,
203213 default => \Predis \Client::class,
204214 },
205- isset ($ params ['redis_sentinel ' ]) => match (true ) {
215+ isset ($ params ['sentinel ' ]) => match (true ) {
206216 \extension_loaded ('redis ' ) => \Redis::class,
207217 \extension_loaded ('relay ' ) => Relay::class,
208218 default => \Predis \Client::class,
@@ -213,7 +223,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
213223 default => \Predis \Client::class,
214224 };
215225
216- if (isset ($ params ['redis_sentinel ' ]) && !is_a ($ class , \Predis \Client::class, true ) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
226+ if (isset ($ params ['sentinel ' ]) && !is_a ($ class , \Predis \Client::class, true ) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
217227 throw new CacheException (\sprintf ('Cannot use Redis Sentinel: class "%s" does not extend "Predis\Client" and neither ext-redis >= 5.2 nor ext-relay have been found. ' , $ class ));
218228 }
219229
@@ -237,7 +247,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
237247 $ host = 'tls:// ' .$ host ;
238248 }
239249
240- if (!isset ($ params ['redis_sentinel ' ])) {
250+ if (!isset ($ params ['sentinel ' ])) {
241251 break ;
242252 }
243253
@@ -263,15 +273,15 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
263273 $ sentinel = @new $ sentinelClass ($ host , $ port , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ], $ params ['read_timeout ' ], ...$ extra );
264274 }
265275
266- if ($ address = @$ sentinel ->getMasterAddrByName ($ params ['redis_sentinel ' ])) {
276+ if ($ address = @$ sentinel ->getMasterAddrByName ($ params ['sentinel ' ])) {
267277 [$ host , $ port ] = $ address ;
268278 }
269279 } catch (\RedisException |\Relay \Exception $ redisException ) {
270280 }
271281 } while (++$ hostIndex < \count ($ hosts ) && !$ address );
272282
273- if (isset ($ params ['redis_sentinel ' ]) && !$ address ) {
274- throw new InvalidArgumentException (\sprintf ('Failed to retrieve master information from sentinel "%s". ' , $ params ['redis_sentinel ' ]), previous: $ redisException ?? null );
283+ if (isset ($ params ['sentinel ' ]) && !$ address ) {
284+ throw new InvalidArgumentException (\sprintf ('Failed to retrieve master information from sentinel "%s". ' , $ params ['sentinel ' ]), previous: $ redisException ?? null );
275285 }
276286
277287 try {
@@ -446,11 +456,14 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
446456
447457 $ redis = $ params ['lazy ' ] ? RedisClusterProxy::createLazyProxy ($ initializer ) : $ initializer ();
448458 } elseif (is_a ($ class , \Predis \ClientInterface::class, true )) {
449- if ($ params ['redis_cluster ' ]) {
459+ if ($ params ['cluster ' ]) {
450460 $ params ['cluster ' ] = 'redis ' ;
451- } elseif (isset ($ params ['redis_sentinel ' ])) {
461+ } else {
462+ unset($ params ['cluster ' ]);
463+ }
464+ if (isset ($ params ['sentinel ' ])) {
452465 $ params ['replication ' ] = 'sentinel ' ;
453- $ params ['service ' ] = $ params ['redis_sentinel ' ];
466+ $ params ['service ' ] = $ params ['sentinel ' ];
454467 }
455468 $ params += ['parameters ' => []];
456469 $ params ['parameters ' ] += [
@@ -478,16 +491,16 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
478491 }
479492 }
480493
481- if (1 === \count ($ hosts ) && !($ params ['redis_cluster ' ] || $ params ['redis_sentinel ' ])) {
494+ if (1 === \count ($ hosts ) && !isset ($ params ['cluster ' ]) & ! isset ( $ params ['sentinel ' ])) {
482495 $ hosts = $ hosts [0 ];
483496 } elseif (\in_array ($ params ['failover ' ], ['slaves ' , 'distribute ' ], true ) && !isset ($ params ['replication ' ])) {
484497 $ params ['replication ' ] = true ;
485498 $ hosts [0 ] += ['alias ' => 'master ' ];
486499 }
487500 $ params ['exceptions ' ] = false ;
488501
489- $ redis = new $ class ($ hosts , array_diff_key ($ params , self ::$ defaultConnectionOptions ));
490- if (isset ($ params ['redis_sentinel ' ])) {
502+ $ redis = new $ class ($ hosts , array_diff_key ($ params , array_diff_key ( self ::$ defaultConnectionOptions, [ ' cluster ' => null ]) ));
503+ if (isset ($ params ['sentinel ' ])) {
491504 $ redis ->getConnection ()->setSentinelTimeout ($ params ['timeout ' ]);
492505 }
493506 } elseif (class_exists ($ class , false )) {
0 commit comments