Redis: Support connection to unix socket

Also handle exceptions thrown on connection and remove support for
specifying hosts with redis:// scheme.
pull/6763/head
Aleksander Machniak 5 years ago
parent a8f3d46bb9
commit 2dccbf2879

@ -7,6 +7,7 @@ CHANGELOG Roundcube Webmail
- Renamed 'log_session' option to 'session_debug'
- Don't log full session identifiers in userlogins log (#6625)
- installto.sh: Add possibility to run the update even on the up-to-date installation (#6533)
- Redis: Support connection to unix socket
- Elastic: Add Prev/Next buttons on message page toolbar (#6648)
- Elastic: Close search options on Enter key press in quick-search input (#6660)
- Elastic: Changed read/unread icons (#6636)

@ -328,7 +328,8 @@ $config['ldap_cache_ttl'] = '10m';
// Use these hosts for accessing memcached
// Define any number of hosts in the form of hostname:port or unix:///path/to/socket.file
$config['memcache_hosts'] = null; // e.g. array( 'localhost:11211', '192.168.1.12:11211', 'unix:///var/tmp/memcached.sock' );
// Example: array('localhost:11211', '192.168.1.12:11211', 'unix:///var/tmp/memcached.sock');
$config['memcache_hosts'] = null;
// Controls the use of a persistent connections to memcache servers
// See http://php.net/manual/en/memcache.addserver.php
@ -343,11 +344,15 @@ $config['memcache_timeout'] = 1;
// See http://php.net/manual/en/memcache.addserver.php
$config['memcache_retry_interval'] = 15;
// use these hosts for accessing Redis.
// Currently only one host is supported. cluster support may come in a future release.
// You can pass 4 fields, host, port, database and password.
// Unset fields will be set to the default values host=127.0.0.1, port=6379, database=0, password= (empty)
$config['redis_hosts'] = null; // e.g. array( 'localhost:6379' ); array( '192.168.1.1:6379:1:secret' );
// Use these hosts for accessing Redis.
// Currently only one host is supported. Cluster support may come in a future release.
// You can pass 4 fields, host, port (optional), database (optional) and password (optional).
// Unset fields will be set to the default values host=127.0.0.1, port=6379.
// Examples:
// array('localhost:6379');
// array('192.168.1.1:6379:1:secret');
// array('unix:///var/run/redis/redis-server.sock:1:secret');
$config['redis_hosts'] = null;
// Maximum size of an object in memcache (in bytes). Default: 2MB
$config['memcache_max_allowed_packet'] = '2M';

@ -83,8 +83,8 @@ class rcube_cache_redis extends rcube_cache
true, true);
}
$rcube = rcube::get_instance();
$hosts = $rcube->config->get('redis_hosts');
$rcube = rcube::get_instance();
$hosts = $rcube->config->get('redis_hosts');
// host config is wrong
if (!is_array($hosts) || empty($hosts)) {
@ -116,54 +116,56 @@ class rcube_cache_redis extends rcube_cache
// explode individual fields
list($host, $port, $database, $password) = array_pad(explode(':', $redis_host, 4), 4, null);
$params = parse_url($redis_host);
if ($params['scheme'] == 'redis') {
$host = (isset($params['host'])) ? $params['host'] : null;
$port = (isset($params['port'])) ? $params['port'] : null;
$database = (isset($params['database'])) ? $params['database'] : null;
$password = (isset($params['password'])) ? $params['password'] : null;
if (substr($redis_host, 0, 7) === 'unix://') {
$host = substr($port, 2);
$port = null;
}
// set default values if not set
$host = ($host !== null) ? $host : '127.0.0.1';
$port = ($port !== null) ? $port : 6379;
$database = ($database !== null) ? $database : 0;
if (self::$redis->connect($host, $port) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not connect to Redis server. Please check host and port"
),
true, true);
else {
// set default values if not set
$host = $host ?: '127.0.0.1';
$port = $port ?: 6379;
}
if ($password != null && self::$redis->auth($password) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not authenticate with Redis server. Please check password."
),
true, true);
try {
if (self::$redis->connect($host, $port) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not connect to Redis server. Please check host and port"
),
true, true);
}
if ($password !== null && self::$redis->auth($password) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not authenticate with Redis server. Please check password."
),
true, true);
}
if ($database !== null && self::$redis->select($database) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not select Redis database. Please check database setting."
),
true, true);
}
}
if ($database != 0 && self::$redis->select($database) === false) {
rcube::raise_error(array(
'code' => 604,
'type' => 'redis',
'line' => __LINE__,
'file' => __FILE__,
'message' => "Could not select Redis database. Please check database setting."
),
true, true);
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
}
if (self::$redis->ping() != "+PONG") {
if (self::$redis->ping() !== "+PONG") {
self::$redis = false;
}

Loading…
Cancel
Save