8000 [Cache] Code cleanup · symfony/symfony@922f5ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 922f5ff

Browse files
[Cache] Code cleanup
1 parent 9522e62 commit 922f5ff

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed

src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testCreateConnection()
4242
'scheme' => 'tcp',
4343
'host' => $redisHost[0],
4444
'port' => (int) ($redisHost[1] ?? 6379),
45-
'persistent' => 0,
45+
'persistent' => false,
4646
'timeout' => 3,
4747
'read_write_timeout' => 0,
4848
'tcp_nodelay' => true,
@@ -74,7 +74,7 @@ public function testCreateSslConnection()
7474
'host' => $redisHost[0],
7575
'port' => (int) ($redisHost[1] ?? 6379),
7676
'ssl' => ['verify_peer' => '0'],
77-
'persistent' => 0,
77+
'persistent' => false,
7878
'timeout' => 3,
7979
'read_write_timeout' => 0,
8080
'tcp_nodelay' => true,

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ trait RedisTrait
3838
{
3939
private static array $defaultConnectionOptions = [
4040
'class' => null,
41-
'persistent' => 0,
41+
'persistent' => false,
4242
'persistent_id' => null,
4343
'timeout' => 30,
4444
'read_timeout' => 0,
45-
'command_timeout' => 0,
4645
'retry_interval' => 0,
4746
'tcp_keepalive' => 0,
4847
'lazy' => null,
4948
'cluster' => false,
49+
'cluster_command_timeout' => 0,
50+
'cluster_relay_context' => [],
5051
'sentinel' => null,
51-
'relay_cluster_context' => [],
5252
'dbindex' => 0,
5353
'failover' => 'none',
5454
'ssl' => null, // see https://php.net/context.ssl
@@ -196,10 +196,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
196196
throw new CacheException('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay".');
197197
}
198198

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+
}
201203
}
202-
$params['cluster'] = filter_var($params['cluster'], \FILTER_VALIDATE_BOOLEAN);
203204

204205
if ($params['cluster'] && isset($params['sentinel'])) {
205206
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
285286

286287
try {
287288
$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,
298290
];
299291

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-
306292
if (isset($params['auth'])) {
307293
$extra['auth'] = $params['auth'];
308294
}
@@ -379,34 +365,27 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
379365< A935 /td>
}
380366

381367
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+
};
400379
}
401380

402381
$relayCluster = new $class(
403382
name: null,
404383
seeds: $hosts,
405384
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'],
408387
auth: $params['auth'] ?? null,
409-
context: $relayClusterContext
388+
context: $context,
410389
);
411390
} catch (\Relay\Exception $e) {
412391
throw new InvalidArgumentException('Relay cluster connection failed: '.$e->getMessage());
@@ -435,7 +414,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra
435414
}
436415

437416
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] : []);
439418
} catch (\RedisClusterException $e) {
440419
throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage());
441420
}
@@ -796,4 +775,17 @@ private function getHosts(): array
796775

797776
return $hosts;
798777
}
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+
}
799791
}

0 commit comments

Comments
 (0)
0