@@ -46,9 +46,10 @@ trait RedisTrait
46
46
'retry_interval ' => 0 ,
47
47
'tcp_keepalive ' => 0 ,
48
48
'lazy ' => null ,
49
- 'redis_cluster ' => false ,
50
- 'relay_cluster_context ' => [],
51
49
'redis_sentinel ' => null ,
50
+ 'cluster ' => false ,
51
+ 'sentinel ' => null ,
52
+ 'relay_cluster_context ' => [],
52
53
'dbindex ' => 0 ,
53
54
'failover ' => 'none ' ,
54
55
'ssl ' => null , // see https://php.net/context.ssl
@@ -90,13 +91,13 @@ private function init(\Redis|Relay|RelayCluster|\RedisArray|\RedisCluster|\Predi
90
91
*/
91
92
public static function createConnection (#[\SensitiveParameter] string $ dsn , array $ options = []): \Redis |\RedisArray |\RedisCluster |\Predis \ClientInterface |Relay |RelayCluster
92
93
{
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
+ };
100
101
101
102
if (!\extension_loaded ('redis ' ) && !class_exists (\Predis \Client::class)) {
102
103
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
124
125
125
126
$ query = $ hosts = [];
126
127
127
- $ tls = 'rediss ' === $ scheme ;
128
+ $ tls = 'rediss ' === $ scheme || ' valkeys ' === $ scheme ;
128
129
$ tcpScheme = $ tls ? 'tls ' : 'tcp ' ;
129
130
130
131
if (isset ($ params ['query ' ])) {
@@ -177,32 +178,41 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
177
178
178
179
$ params += $ query + $ options + self ::$ defaultConnectionOptions ;
179
180
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
+ };
182
194
}
183
195
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)) {
187
197
throw new CacheException ('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay". ' );
188
198
}
189
199
190
200
if (isset ($ params ['lazy ' ])) {
191
201
$ params ['lazy ' ] = filter_var ($ params ['lazy ' ], \FILTER_VALIDATE_BOOLEAN );
192
202
}
203
+ $ params ['cluster ' ] = filter_var ($ params ['cluster ' ], \FILTER_VALIDATE_BOOLEAN );
193
204
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. ' );
197
207
}
198
208
199
209
$ class = $ params ['class ' ] ?? match (true ) {
200
- $ params ['redis_cluster ' ] => match (true ) {
210
+ $ params ['cluster ' ] => match (true ) {
201
211
\extension_loaded ('redis ' ) => \RedisCluster::class,
202
212
\extension_loaded ('relay ' ) => RelayCluster::class,
203
213
default => \Predis \Client::class,
204
214
},
205
- isset ($ params ['redis_sentinel ' ]) => match (true ) {
215
+ isset ($ params ['sentinel ' ]) => match (true ) {
206
216
\extension_loaded ('redis ' ) => \Redis::class,
207
217
\extension_loaded ('relay ' ) => Relay::class,
208
218
default => \Predis \Client::class,
@@ -213,7 +223,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
213
223
default => \Predis \Client::class,
214
224
};
215
225
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)) {
217
227
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 ));
218
228
}
219
229
@@ -237,7 +247,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
237
247
$ host = 'tls:// ' .$ host ;
238
248
}
239
249
240
- if (!isset ($ params ['redis_sentinel ' ])) {
250
+ if (!isset ($ params ['sentinel ' ])) {
241
251
break ;
242
252
}
243
253
@@ -263,15 +273,15 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
263
273
$ sentinel = @new $ sentinelClass ($ host , $ port , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ], $ params ['read_timeout ' ], ...$ extra );
264
274
}
265
275
266
- if ($ address = @$ sentinel ->getMasterAddrByName ($ params ['redis_sentinel ' ])) {
276
+ if ($ address = @$ sentinel ->getMasterAddrByName ($ params ['sentinel ' ])) {
267
277
[$ host , $ port ] = $ address ;
268
278
}
269
279
} catch (\RedisException |\Relay \Exception $ redisException ) {
270
280
}
271
281
} while (++$ hostIndex < \count ($ hosts ) && !$ address );
272
282
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 );
275
285
}
276
286
277
287
try {
@@ -446,11 +456,14 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
446
456
447
457
$ redis = $ params ['lazy ' ] ? RedisClusterProxy::createLazyProxy ($ initializer ) : $ initializer ();
448
458
} elseif (is_a ($ class , \Predis \ClientInterface::class, true )) {
449
- if ($ params ['redis_cluster ' ]) {
459
+ if ($ params ['cluster ' ]) {
450
460
$ params ['cluster ' ] = 'redis ' ;
451
- } elseif (isset ($ params ['redis_sentinel ' ])) {
461
+ } else {
462
+ unset($ params ['cluster ' ]);
463
+ }
464
+ if (isset ($ params ['sentinel ' ])) {
452
465
$ params ['replication ' ] = 'sentinel ' ;
453
- $ params ['service ' ] = $ params ['redis_sentinel ' ];
466
+ $ params ['service ' ] = $ params ['sentinel ' ];
454
467
}
455
468
$ params += ['parameters ' => []];
456
469
$ params ['parameters ' ] += [
@@ -478,16 +491,16 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
478
491
}
479
492
}
480
493
481
- if (1 === \count ($ hosts ) && !($ params ['redis_cluster ' ] || $ params ['redis_sentinel ' ])) {
494
+ if (1 === \count ($ hosts ) && !isset ($ params ['cluster ' ]) & ! isset ( $ params ['sentinel ' ])) {
482
495
$ hosts = $ hosts [0 ];
483
496
} elseif (\in_array ($ params ['failover ' ], ['slaves ' , 'distribute ' ], true ) && !isset ($ params ['replication ' ])) {
484
497
$ params ['replication ' ] = true ;
485
498
$ hosts [0 ] += ['alias ' => 'master ' ];
486
499
}
487
500
$ params ['exceptions ' ] = false ;
488
501
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 ' ])) {
491
504
$ redis ->getConnection ()->setSentinelTimeout ($ params ['timeout ' ]);
492
505
}
493
506
} elseif (class_exists ($ class , false )) {
0 commit comments