Skip to content

[Cache] Code cleanup #59911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
80 changes: 36 additions & 44 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
}
}
Loading