diff --git a/Cache/Frontend/Adapter/SymfonyAdapterProvider.php b/Cache/Frontend/Adapter/SymfonyAdapterProvider.php index ae9233ab01d..d07d0737752 100644 --- a/Cache/Frontend/Adapter/SymfonyAdapterProvider.php +++ b/Cache/Frontend/Adapter/SymfonyAdapterProvider.php @@ -350,12 +350,18 @@ class SymfonyAdapterProvider implements ResetAfterRequestInterface ?int $retryInterval, ?int $connectRetries ) { + $socketPath = $this->resolveUnixSocketPath($host); + // Build optimized DSN with all connection parameters $dsnParams = []; // Add persistent connection parameters if ($persistent) { $dsnParams[] = 'persistent=1'; + if ($socketPath !== null) { + // Keep persistent socket connections isolated by database and process. + $persistentId = ($persistentId ?? 'default') . ':' . $database . ':' . getmypid(); + } if ($persistentId) { $dsnParams[] = 'persistent_id=' . urlencode($persistentId); } @@ -380,9 +386,15 @@ class SymfonyAdapterProvider implements ResetAfterRequestInterface } // Build base DSN - $baseDsn = $password - ? sprintf('redis://%s@%s:%d/%d', urlencode($password), $host, $port, $database) - : sprintf('redis://%s:%d/%d', $host, $port, $database); + if ($socketPath !== null) { + $address = sprintf('%s/%d', $socketPath, $database); + $encodedPassword = rawurlencode($password ?? ''); + } else { + $address = sprintf('%s:%d/%d', $host, $port, $database); + $encodedPassword = urlencode($password ?? ''); + } + $auth = $password ? $encodedPassword . '@' : ''; + $baseDsn = 'redis://' . $auth . $address; // Append DSN parameters $dsn = $dsnParams ? $baseDsn . '?' . implode('&', $dsnParams) : $baseDsn; @@ -391,6 +403,18 @@ class SymfonyAdapterProvider implements ResetAfterRequestInterface return RedisAdapter::createConnection($dsn); } + /** + * Resolve a Unix socket path from a backend server value. + * + * @param string $host + * @return string|null + */ + private function resolveUnixSocketPath(string $host): ?string + { + $path = str_starts_with($host, 'unix://') ? substr($host, strlen('unix://')) : $host; + return str_starts_with($path, '/') ? $path : null; + } + /** * Create ultra-optimized Predis connection (Symfony-compatible, maximum performance) * @@ -413,12 +437,19 @@ class SymfonyAdapterProvider implements ResetAfterRequestInterface ?float $timeout, ?float $readTimeout ) { - $params = [ - 'scheme' => 'tcp', - 'host' => $host, - 'port' => $port, - 'database' => $database, - ]; + $socketPath = $this->resolveUnixSocketPath($host); + $params = $socketPath !== null + ? [ + 'scheme' => 'unix', + 'path' => $socketPath, + 'database' => $database, + ] + : [ + 'scheme' => 'tcp', + 'host' => $host, + 'port' => $port, + 'database' => $database, + ]; if ($password) { $params['password'] = $password;