@@ -44,8 +44,8 @@ trait RedisTrait
44
44
'retry_interval ' => 0 ,
45
45
'tcp_keepalive ' => 0 ,
46
46
'lazy ' => null ,
47
- 'redis_cluster ' => false ,
48
- 'redis_sentinel ' => null ,
47
+ 'cluster ' => false ,
48
+ 'sentinel ' => null ,
49
49
'dbindex ' => 0 ,
50
50
'failover ' => 'none ' ,
51
51
'ssl ' => null , // see https://php.net/context.ssl
@@ -87,13 +87,13 @@ private function init(\Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInter
87
87
*/
88
88
public static function createConnection (#[\SensitiveParameter] string $ dsn , array $ options = []): \Redis |\RedisArray |\RedisCluster |\Predis \ClientInterface |Relay
89
89
{
90
- if ( str_starts_with ( $ dsn , ' redis: ' ) ) {
91
- $ scheme = 'redis ' ;
92
- } elseif ( str_starts_with ($ dsn , 'rediss: ' )) {
93
- $ scheme = ' rediss ' ;
94
- } else {
95
- throw new InvalidArgumentException ('Invalid Redis DSN: it does not start with "redis[s]:". ' );
96
- }
90
+ $ scheme = match ( true ) {
91
+ str_starts_with ( $ dsn , ' redis: ' ) => 'redis ' ,
92
+ str_starts_with ($ dsn , 'rediss: ' ) => ' rediss ' ,
93
+ str_starts_with ( $ dsn , ' valkey: ' ) => ' valkey ' ,
94
+ str_starts_with ( $ dsn , ' valkeys: ' ) => ' valkeys ' ,
95
+ default => throw new InvalidArgumentException ('Invalid Redis DSN: it does not start with "redis[s]:" nor "valkey[s]:" . ' ),
96
+ };
97
97
98
98
if (!\extension_loaded ('redis ' ) && !class_exists (\Predis \Client::class)) {
99
99
throw new CacheException ('Cannot find the "redis" extension nor the "predis/predis" package. ' );
@@ -121,7 +121,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
121
121
122
122
$ query = $ hosts = [];
123
123
124
- $ tls = 'rediss ' === $ scheme ;
124
+ $ tls = 'rediss ' === $ scheme || ' valkeys ' === $ scheme ;
125
125
$ tcpScheme = $ tls ? 'tls ' : 'tcp ' ;
126
126
127
127
if (isset ($ params ['query ' ])) {
@@ -174,28 +174,37 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
174
174
175
175
$ params += $ query + $ options + self ::$ defaultConnectionOptions ;
176
176
177
- if (isset ($ params ['redis_sentinel ' ]) && isset ($ params ['sentinel_master ' ])) {
178
- throw new InvalidArgumentException ('Cannot use both "redis_sentinel" and "sentinel_master" at the same time. ' );
177
+ $ aliases = [
178
+ 'sentinel_master ' => 'sentinel ' ,
179
+ 'redis_sentinel ' => 'sentinel ' ,
180
+ 'redis_cluster ' => 'cluster ' ,
181
+ ];
182
+ foreach ($ aliases as $ alias => $ key ) {
183
+ $ params [$ key ] = match (true ) {
184
+ \array_key_exists ($ key , $ query ) => $ query [$ key ],
185
+ \array_key_exists ($ alias , $ query ) => $ query [$ alias ],
186
+ \array_key_exists ($ key , $ options ) => $ options [$ key ],
187
+ \array_key_exists ($ alias , $ options ) => $ options [$ alias ],
188
+ default => $ params [$ key ],
189
+ };
179
190
}
180
191
181
- $ params ['redis_sentinel ' ] ??= $ params ['sentinel_master ' ] ?? null ;
182
-
183
- if (isset ($ params ['redis_sentinel ' ]) && !class_exists (\Predis \Client::class) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
192
+ if (isset ($ params ['sentinel ' ]) && !class_exists (\Predis \Client::class) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
184
193
throw new CacheException ('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay". ' );
185
194
}
186
195
187
196
if (isset ($ params ['lazy ' ])) {
188
197
$ params ['lazy ' ] = filter_var ($ params ['lazy ' ], \FILTER_VALIDATE_BOOLEAN );
189
198
}
190
- $ params ['redis_cluster ' ] = filter_var ($ params ['redis_cluster ' ], \FILTER_VALIDATE_BOOLEAN );
199
+ $ params ['cluster ' ] = filter_var ($ params ['cluster ' ], \FILTER_VALIDATE_BOOLEAN );
191
200
192
- if ($ params ['redis_cluster ' ] && isset ($ params ['redis_sentinel ' ])) {
193
- throw new InvalidArgumentException ('Cannot use both "redis_cluster " and "redis_sentinel " at the same time. ' );
201
+ if ($ params ['cluster ' ] && isset ($ params ['sentinel ' ])) {
202
+ throw new InvalidArgumentException ('Cannot use both "cluster " and "sentinel " at the same time. ' );
194
203
}
195
204
196
205
$ class = $ params ['class ' ] ?? match (true ) {
197
- $ params ['redis_cluster ' ] => \extension_loaded ('redis ' ) ? \RedisCluster::class : \Predis \Client::class,
198
- isset ($ params ['redis_sentinel ' ]) => match (true ) {
206
+ $ params ['cluster ' ] => \extension_loaded ('redis ' ) ? \RedisCluster::class : \Predis \Client::class,
207
+ isset ($ params ['sentinel ' ]) => match (true ) {
199
208
\extension_loaded ('redis ' ) => \Redis::class,
200
209
\extension_loaded ('relay ' ) => Relay::class,
201
210
default => \Predis \Client::class,
@@ -206,7 +215,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
206
215
default => \Predis \Client::class,
207
216
};
208
217
209
- if (isset ($ params ['redis_sentinel ' ]) && !is_a ($ class , \Predis \Client::class, true ) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
218
+ if (isset ($ params ['sentinel ' ]) && !is_a ($ class , \Predis \Client::class, true ) && !class_exists (\RedisSentinel::class) && !class_exists (Sentinel::class)) {
210
219
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 ));
211
220
}
212
221
@@ -230,7 +239,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
230
239
$ host = 'tls:// ' .$ host ;
231
240
}
232
241
233
- if (!isset ($ params ['redis_sentinel ' ])) {
242
+ if (!isset ($ params ['sentinel ' ])) {
234
243
break ;
235
244
}
236
245
@@ -256,15 +265,15 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
256
265
$ sentinel = @new $ sentinelClass ($ host , $ port , $ params ['timeout ' ], (string ) $ params ['persistent_id ' ], $ params ['retry_interval ' ], $ params ['read_timeout ' ], ...$ extra );
257
266
}
258
267
259
- if ($ address = @$ sentinel ->getMasterAddrByName ($ params ['redis_sentinel ' ])) {
268
+ if ($ address = @$ sentinel ->getMasterAddrByName ($ params ['sentinel ' ])) {
260
269
[$ host , $ port ] = $ address ;
261
270
}
262
271
} catch (\RedisException |\Relay \Exception $ redisException ) {
263
272
}
264
273
} while (++$ hostIndex < \count ($ hosts ) && !$ address );
265
274
266
- if (isset ($ params ['redis_sentinel ' ]) && !$ address ) {
267
- throw new InvalidArgumentException (\sprintf ('Failed to retrieve master information from sentinel "%s". ' , $ params ['redis_sentinel ' ]), previous: $ redisException ?? null );
275
+ if (isset ($ params ['sentinel ' ]) && !$ address ) {
276
+ throw new InvalidArgumentException (\sprintf ('Failed to retrieve master information from sentinel "%s". ' , $ params ['sentinel ' ]), previous: $ redisException ?? null );
268
277
}
269
278
270
279
try {
@@ -379,11 +388,14 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
379
388
380
389
$ redis = $ params ['lazy ' ] ? RedisClusterProxy::createLazyProxy ($ initializer ) : $ initializer ();
381
390
} elseif (is_a ($ class , \Predis \ClientInterface::class, true )) {
382
- if ($ params ['redis_cluster ' ]) {
391
+ if ($ params ['cluster ' ]) {
383
392
$ params ['cluster ' ] = 'redis ' ;
384
- } elseif (isset ($ params ['redis_sentinel ' ])) {
393
+ } else {
394
+ unset($ params ['cluster ' ]);
395
+ }
396
+ if (isset ($ params ['sentinel ' ])) {
385
397
$ params ['replication ' ] = 'sentinel ' ;
386
- $ params ['service ' ] = $ params ['redis_sentinel ' ];
398
+ $ params ['service ' ] = $ params ['sentinel ' ];
387
399
}
388
400
$ params += ['parameters ' => []];
389
401
$ params ['parameters ' ] += [
@@ -411,16 +423,16 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
411
423
}
412
424
}
413
425
414
- if (1 === \count ($ hosts ) && !($ params ['redis_cluster ' ] || $ params ['redis_sentinel ' ])) {
426
+ if (1 === \count ($ hosts ) && !isset ($ params ['cluster ' ]) & ! isset ( $ params ['sentinel ' ])) {
415
427
$ hosts = $ hosts [0 ];
416
428
} elseif (\in_array ($ params ['failover ' ], ['slaves ' , 'distribute ' ], true ) && !isset ($ params ['replication ' ])) {
417
429
$ params ['replication ' ] = true ;
418
430
$ hosts [0 ] += ['alias ' => 'master ' ];
419
431
}
420
432
$ params ['exceptions ' ] = false ;
421
433
422
- $ redis = new $ class ($ hosts , array_diff_key ($ params , self ::$ defaultConnectionOptions ));
423
- if (isset ($ params ['redis_sentinel ' ])) {
434
+ $ redis = new $ class ($ hosts , array_diff_key ($ params , array_diff_key ( self ::$ defaultConnectionOptions, [ ' cluster ' => null ]) ));
435
+ if (isset ($ params ['sentinel ' ])) {
424
436
$ redis ->getConnection ()->setSentinelTimeout ($ params ['timeout ' ]);
425
437
}
426
438
} elseif (class_exists ($ class , false )) {
0 commit comments