From 762bd7a8473f445f1e89ab386f55f71c94221f98 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 2 Aug 2023 20:42:44 +0200 Subject: [PATCH] Add support for Unix domain sockets to Redis in setting defaults - Add support for port = 0 in Redis driver --- src/Core/Cache/Type/RedisCache.php | 21 +++++++++++++-------- static/defaults.config.php | 6 +++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Core/Cache/Type/RedisCache.php b/src/Core/Cache/Type/RedisCache.php index be2a7ff080..a6ce7c44b2 100644 --- a/src/Core/Cache/Type/RedisCache.php +++ b/src/Core/Cache/Type/RedisCache.php @@ -57,22 +57,27 @@ class RedisCache extends AbstractCache implements ICanCacheInMemory $redis_host = $config->get('system', 'redis_host'); $redis_port = $config->get('system', 'redis_port'); $redis_pw = $config->get('system', 'redis_password'); - $redis_db = $config->get('system', 'redis_db', 0); + $redis_db = (int)$config->get('system', 'redis_db', 0); try { - - if (!empty($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) { - throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available'); - } else if (!@$this->redis->connect($redis_host)) { - throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ' isn\'t available'); + if (is_numeric($redis_port) && $redis_port > -1) { + $connection_string = $redis_host . ':' . $redis_port; + if (!@$this->redis->connect($redis_host, $redis_port)) { + throw new CachePersistenceException('Expected Redis server at ' . $connection_string . " isn't available"); + } + } else { + $connection_string = $redis_host; + if (!@$this->redis->connect($redis_host)) { + throw new CachePersistenceException('Expected Redis server at ' . $connection_string . ' isn\'t available'); + } } if (!empty($redis_pw) && !$this->redis->auth($redis_pw)) { - throw new CachePersistenceException('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port); + throw new CachePersistenceException('Cannot authenticate redis server at ' . $connection_string); } if ($redis_db !== 0 && !$this->redis->select($redis_db)) { - throw new CachePersistenceException('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port); + throw new CachePersistenceException('Cannot switch to redis db ' . $redis_db . ' at ' . $connection_string); } } catch (\RedisException $exception) { throw new CachePersistenceException('Redis connection fails unexpectedly', $exception); diff --git a/static/defaults.config.php b/static/defaults.config.php index e8f44613ce..6c9e2dd3ce 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -543,11 +543,11 @@ return [ 'pushpoll_frequency' => 3, // redis_host (String) - // Host name of the redis daemon. + // Host name or the path to the Unix domain socket of the Redis daemon. 'redis_host' => '127.0.0.1', - // redis_port (String) - // Port number of the redis daemon. + // redis_port (Integer) + // Port number of the Redis daemon, should be -1 for unix domain socket 'redis_port' => 6379, // redis_db (Integer)