@@ -38,17 +38,17 @@ trait RedisTrait
38
38
{
39
39
private static array $ defaultConnectionOptions = [
40
40
'class ' => null ,
41
- 'persistent ' => 0 ,
41
+ 'persistent ' => false ,
42
42
'persistent_id ' => null ,
43
43
'timeout ' => 30 ,
44
44
'read_timeout ' => 0 ,
45
- 'command_timeout ' => 0 ,
46
45
'retry_interval ' => 0 ,
47
46
'tcp_keepalive ' => 0 ,
48
47
'lazy ' => null ,
49
48
'cluster ' => false ,
49
+ 'cluster_command_timeout ' => 0 ,
50
+ 'cluster_relay_context ' => [],
50
51
'sentinel ' => null ,
51
- 'relay_cluster_context ' => [],
52
52
'dbindex ' => 0 ,
53
53
'failover ' => 'none ' ,
54
54
'ssl ' => null , // see https://php.net/context.ssl
@@ -196,10 +196,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
196
196
throw new CacheException ('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay". ' );
197
197
}
198
198
199
- if (isset ($ params ['lazy ' ])) {
200
- $ params ['lazy ' ] = filter_var ($ params ['lazy ' ], \FILTER_VALIDATE_BOOLEAN );
199
+ foreach (['lazy ' , 'persistent ' , 'cluster ' ] as $ option ) {
200
+ if (!\is_bool ($ params [$ option ] ?? false )) {
201
+ $ params [$ option ] = filter_var ($ params [$ option ], \FILTER_VALIDATE_BOOLEAN );
202
+ }
201
203
}
202
- $ params ['cluster ' ] = filter_var ($ params ['cluster ' ], \FILTER_VALIDATE_BOOLEAN );
203
204
204
205
if ($ params ['cluster ' ] && isset ($ params ['sentinel ' ])) {
205
206
throw new InvalidArgumentException ('Cannot use both "cluster" and "sentinel" at the same time. ' );
@@ -285,24 +286,9 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
285
286
286
287
try {
287
288
$ extra = [
288
- 'stream ' => $ params ['ssl ' ] ?? null ,
289
- ];
290
- $ booleanStreamOptions = [
291
- 'allow_self_signed ' ,
292
- 'capture_peer_cert ' ,
293
- 'capture_peer_cert_chain ' ,
294
- 'disable_compression ' ,
295
- 'SNI_enabled ' ,
296
- 'verify_peer ' ,
297
- 'verify_peer_name ' ,
289
+ 'stream ' => self ::filterSslOptions ($ params ['ssl ' ] ?? []) ?: null ,
298
290
];
299
291
300
- foreach ($ extra ['stream ' ] ?? [] as $ streamOption => $ value ) {
301
- if (\in_array ($ streamOption , $ booleanStreamOptions , true ) && \is_string ($ value )) {
302
- $ extra ['stream ' ][$ streamOption ] = filter_var ($ value , \FILTER_VALIDATE_BOOL );
303
- }
304
- }
305
-
306
292
if (isset ($ params ['auth ' ])) {
307
293
$ extra ['auth ' ] = $ params ['auth ' ];
308
294
}
@@ -379,34 +365,27 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
379
365
<
A935
/td> }
380
366
381
367
try {
382
- $ relayClusterContext = $ params ['relay_cluster_context ' ];
383
-
384
- foreach (['allow_self_signed ' , 'verify_peer_name ' , 'verify_peer ' ] as $ contextStreamBoolField ) {
385
- if (isset ($ relayClusterContext ['stream ' ][$ contextStreamBoolField ])) {
386
- $ relayClusterContext ['stream ' ][$ contextStreamBoolField ] = filter_var ($ relayClusterContext ['stream ' ][$ contextStreamBoolField ], \FILTER_VALIDATE_BOOL );
387
- }
388
- }
389
-
390
- foreach (['use-cache ' , 'client-tracking ' , 'throw-on-error ' , 'client-invalidations ' , 'reply-literal ' , 'persistent ' ] as $ contextBoolField ) {
391
- if (isset ($ relayClusterContext [$ contextBoolField ])) {
392
- $ relayClusterContext [$ contextBoolField ] = filter_var ($ relayClusterContext [$ contextBoolField ], \FILTER_VALIDATE_BOOL );
393
- }
394
- }
395
-
396
- foreach (['max-retries ' , 'serializer ' , 'compression ' , 'compression-level ' ] as $ contextIntField ) {
397
- if (isset ($ relayClusterContext [$ contextIntField ])) {
398
- $ relayClusterContext [$ contextIntField ] = filter_var ($ relayClusterContext [$ contextIntField ], \FILTER_VALIDATE_INT );
399
- }
368
+ $ context = $ params ['cluster_relay_context ' ];
369
+ $ context ['stream ' ] = self ::filterSslOptions ($ params ['ssl ' ] ?? []) ?: null ;
370
+
371
+ foreach ($ context as $ name => $ value ) {
372
+ match ($ name ) {
373
+ 'use-cache ' , 'client-tracking ' , 'throw-on-error ' , 'client-invalidations ' , 'reply-literal ' , 'persistent ' ,
374
+ => $ context [$ name ] = filter_var ($ value , \FILTER_VALIDATE_BOOLEAN ),
375
+ 'max-retries ' , 'serializer ' , 'compression ' , 'compression-level ' ,
376
+ => $ context [$ name ] = filter_var ($ value , \FILTER_VALIDATE_INT ),
377
+ default => null ,
378
+ };
400
379
}
401
380
402
381
$ relayCluster = new $ class (
403
382
name: null ,
404
383
seeds: $ hosts ,
405
384
connect_timeout: $ params ['timeout ' ],
406
- command_timeout: $ params ['command_timeout ' ],
407
- persistent: ( bool ) $ params ['persistent ' ],
385
+ command_timeout: $ params ['cluster_command_timeout ' ],
386
+ persistent: $ params ['persistent ' ],
408
387
auth: $ params ['auth ' ] ?? null ,
409
- context: $ relayClusterContext
388
+ context: $ context ,
410
389
);
411
390
} catch (\Relay \Exception $ e ) {
412
391
throw new InvalidArgumentException ('Relay cluster connection failed: ' .$ e ->getMessage ());
@@ -435,7 +414,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
435
414
}
436
415
437
416
try {
438
- $ redis = new $ class (null , $ hosts , $ params ['timeout ' ], $ params ['read_timeout ' ], ( bool ) $ params ['persistent ' ], $ params ['auth ' ] ?? '' , ...\defined ('Redis::SCAN_PREFIX ' ) ? [$ params ['ssl ' ] ?? null ] : []);
417
+ $ redis = new $ class (null , $ hosts , $ params ['timeout ' ], $ params ['read_timeout ' ], $ params ['persistent ' ], $ params ['auth ' ] ?? '' , ...\defined ('Redis::SCAN_PREFIX ' ) ? [$ params ['ssl ' ] ?? null ] : []);
439
418
} catch (\RedisClusterException $ e ) {
440
419
throw new InvalidArgumentException ('Redis connection failed: ' .$ e ->getMessage ());
441
420
}
@@ -796,4 +775,17 @@ private function getHosts(): array
796
775
797
776
return $ hosts ;
798
777
}
778
+
779
+ private static function filterSslOptions (array $ options ): array
780
+ {
781
+ foreach ($ options as $ name => $ value ) {
782
+ match ($ name ) {
783
+ 'allow_self_signed ' , 'capture_peer_cert ' , 'capture_peer_cert_chain ' , 'disable_compression ' , 'SNI_enabled ' , 'verify_peer ' , 'verify_peer_name ' ,
784
+ => $ options [$ name ] = filter_var ($ value , \FILTER_VALIDATE_BOOLEAN ),
785
+ default => null ,
786
+ };
787
+ }
788
+
789
+ return $ options ;
790
+ }
799
791
}
0 commit comments