Add support for Unix domain sockets to Redis in setting defaults

- Add support for port = 0 in Redis driver
This commit is contained in:
Hypolite Petovan 2023-08-02 20:42:44 +02:00
parent 6478b5ba5f
commit 762bd7a847
2 changed files with 16 additions and 11 deletions

View File

@ -57,22 +57,27 @@ class RedisCache extends AbstractCache implements ICanCacheInMemory
$redis_host = $config->get('system', 'redis_host'); $redis_host = $config->get('system', 'redis_host');
$redis_port = $config->get('system', 'redis_port'); $redis_port = $config->get('system', 'redis_port');
$redis_pw = $config->get('system', 'redis_password'); $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 { try {
if (is_numeric($redis_port) && $redis_port > -1) {
if (!empty($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) { $connection_string = $redis_host . ':' . $redis_port;
throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available'); if (!@$this->redis->connect($redis_host, $redis_port)) {
} else if (!@$this->redis->connect($redis_host)) { throw new CachePersistenceException('Expected Redis server at ' . $connection_string . " isn't available");
throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ' 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)) { 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)) { 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) { } catch (\RedisException $exception) {
throw new CachePersistenceException('Redis connection fails unexpectedly', $exception); throw new CachePersistenceException('Redis connection fails unexpectedly', $exception);

View File

@ -543,11 +543,11 @@ return [
'pushpoll_frequency' => 3, 'pushpoll_frequency' => 3,
// redis_host (String) // 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_host' => '127.0.0.1',
// redis_port (String) // redis_port (Integer)
// Port number of the redis daemon. // Port number of the Redis daemon, should be -1 for unix domain socket
'redis_port' => 6379, 'redis_port' => 6379,
// redis_db (Integer) // redis_db (Integer)