From 922f5ff77ea5dd2fb35262579adf622555b257c0 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 5 Mar 2025 10:13:22 +0100 Subject: [PATCH] [Cache] Code cleanup --- .../Cache/Tests/Adapter/PredisAdapterTest.php | 4 +- .../Component/Cache/Traits/RedisTrait.php | 80 +++++++++---------- 2 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php index d9afd85a8e1f6..730bde7195fb1 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php @@ -42,7 +42,7 @@ public function testCreateConnection() 'scheme' => 'tcp', 'host' => $redisHost[0], 'port' => (int) ($redisHost[1] ?? 6379), - 'persistent' => 0, + 'persistent' => false, 'timeout' => 3, 'read_write_timeout' => 0, 'tcp_nodelay' => true, @@ -74,7 +74,7 @@ public function testCreateSslConnection() 'host' => $redisHost[0], 'port' => (int) ($redisHost[1] ?? 6379), 'ssl' => ['verify_peer' => '0'], - 'persistent' => 0, + 'persistent' => false, 'timeout' => 3, 'read_write_timeout' => 0, 'tcp_nodelay' => true, diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index 3d1a0d937882a..55e7cea614f75 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -38,17 +38,17 @@ trait RedisTrait { private static array $defaultConnectionOptions = [ 'class' => null, - 'persistent' => 0, + 'persistent' => false, 'persistent_id' => null, 'timeout' => 30, 'read_timeout' => 0, - 'command_timeout' => 0, 'retry_interval' => 0, 'tcp_keepalive' => 0, 'lazy' => null, 'cluster' => false, + 'cluster_command_timeout' => 0, + 'cluster_relay_context' => [], 'sentinel' => null, - 'relay_cluster_context' => [], 'dbindex' => 0, 'failover' => 'none', 'ssl' => null, // see https://php.net/context.ssl @@ -196,10 +196,11 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra throw new CacheException('Redis Sentinel support requires one of: "predis/predis", "ext-redis >= 5.2", "ext-relay".'); } - if (isset($params['lazy'])) { - $params['lazy'] = filter_var($params['lazy'], \FILTER_VALIDATE_BOOLEAN); + foreach (['lazy', 'persistent', 'cluster'] as $option) { + if (!\is_bool($params[$option] ?? false)) { + $params[$option] = filter_var($params[$option], \FILTER_VALIDATE_BOOLEAN); + } } - $params['cluster'] = filter_var($params['cluster'], \FILTER_VALIDATE_BOOLEAN); if ($params['cluster'] && isset($params['sentinel'])) { 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 try { $extra = [ - 'stream' => $params['ssl'] ?? null, - ]; - $booleanStreamOptions = [ - 'allow_self_signed', - 'capture_peer_cert', - 'capture_peer_cert_chain', - 'disable_compression', - 'SNI_enabled', - 'verify_peer', - 'verify_peer_name', + 'stream' => self::filterSslOptions($params['ssl'] ?? []) ?: null, ]; - foreach ($extra['stream'] ?? [] as $streamOption => $value) { - if (\in_array($streamOption, $booleanStreamOptions, true) && \is_string($value)) { - $extra['stream'][$streamOption] = filter_var($value, \FILTER_VALIDATE_BOOL); - } - } - if (isset($params['auth'])) { $extra['auth'] = $params['auth']; } @@ -379,34 +365,27 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra } try { - $relayClusterContext = $params['relay_cluster_context']; - - foreach (['allow_self_signed', 'verify_peer_name', 'verify_peer'] as $contextStreamBoolField) { - if (isset($relayClusterContext['stream'][$contextStreamBoolField])) { - $relayClusterContext['stream'][$contextStreamBoolField] = filter_var($relayClusterContext['stream'][$contextStreamBoolField], \FILTER_VALIDATE_BOOL); - } - } - - foreach (['use-cache', 'client-tracking', 'throw-on-error', 'client-invalidations', 'reply-literal', 'persistent'] as $contextBoolField) { - if (isset($relayClusterContext[$contextBoolField])) { - $relayClusterContext[$contextBoolField] = filter_var($relayClusterContext[$contextBoolField], \FILTER_VALIDATE_BOOL); - } - } - - foreach (['max-retries', 'serializer', 'compression', 'compression-level'] as $contextIntField) { - if (isset($relayClusterContext[$contextIntField])) { - $relayClusterContext[$contextIntField] = filter_var($relayClusterContext[$contextIntField], \FILTER_VALIDATE_INT); - } + $context = $params['cluster_relay_context']; + $context['stream'] = self::filterSslOptions($params['ssl'] ?? []) ?: null; + + foreach ($context as $name => $value) { + match ($name) { + 'use-cache', 'client-tracking', 'throw-on-error', 'client-invalidations', 'reply-literal', 'persistent', + => $context[$name] = filter_var($value, \FILTER_VALIDATE_BOOLEAN), + 'max-retries', 'serializer', 'compression', 'compression-level', + => $context[$name] = filter_var($value, \FILTER_VALIDATE_INT), + default => null, + }; } $relayCluster = new $class( name: null, seeds: $hosts, connect_timeout: $params['timeout'], - command_timeout: $params['command_timeout'], - persistent: (bool) $params['persistent'], + command_timeout: $params['cluster_command_timeout'], + persistent: $params['persistent'], auth: $params['auth'] ?? null, - context: $relayClusterContext + context: $context, ); } catch (\Relay\Exception $e) { throw new InvalidArgumentException('Relay cluster connection failed: '.$e->getMessage()); @@ -435,7 +414,7 @@ public static function createConnection(#[\SensitiveParameter] string $dsn, arra } try { - $redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '', ...\defined('Redis::SCAN_PREFIX') ? [$params['ssl'] ?? null] : []); + $redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], $params['persistent'], $params['auth'] ?? '', ...\defined('Redis::SCAN_PREFIX') ? [$params['ssl'] ?? null] : []); } catch (\RedisClusterException $e) { throw new InvalidArgumentException('Redis connection failed: '.$e->getMessage()); } @@ -796,4 +775,17 @@ private function getHosts(): array return $hosts; } + + private static function filterSslOptions(array $options): array + { + foreach ($options as $name => $value) { + match ($name) { + 'allow_self_signed', 'capture_peer_cert', 'capture_peer_cert_chain', 'disable_compression', 'SNI_enabled', 'verify_peer', 'verify_peer_name', + => $options[$name] = filter_var($value, \FILTER_VALIDATE_BOOLEAN), + default => null, + }; + } + + return $options; + } }