diff --git a/administrator/components/com_config/model/form/application.xml b/administrator/components/com_config/model/form/application.xml index de47a993343ee..cfe9c6b1984a7 100644 --- a/administrator/components/com_config/model/form/application.xml +++ b/administrator/components/com_config/model/form/application.xml @@ -922,6 +922,20 @@ size="5" /> + + + + + + + _server = array( - 'host' => $config->get('session_redis_server_host', 'localhost'), - 'port' => $config->get('session_redis_server_port', 6379), - 'db' => (int) $config->get('session_redis_server_db', 0) + 'host' => $config->get('session_redis_server_host', 'localhost'), + 'port' => $config->get('session_redis_server_port', 6379), + 'persist' => $config->get('session_redis_persist', true), + 'auth' => $config->get('session_redis_server_auth', null), + 'db' => (int) $config->get('session_redis_server_db', 0), ); + // If you are trying to connect to a socket file, ignore the supplied port + if ($this->_server['host'][0] === '/') + { + $this->_server['port'] = 0; + } + parent::__construct($options); } @@ -51,17 +59,29 @@ public function __construct($options = array()) */ public function register() { - if (!empty($this->_server) && isset($this->_server['host']) && isset($this->_server['port'])) + if (!empty($this->_server) && isset($this->_server['host'], $this->_server['port'])) { if (!headers_sent()) { - $path = $this->_server['host'] . ":" . $this->_server['port']; + if ($this->_server['port'] === 0) + { + $path = 'unix://' . $this->_server['host']; + } + else + { + $path = 'tcp://' . $this->_server['host'] . ":" . $this->_server['port']; + } + + $persist = isset($this->_server['persist']) ? $this->_server['persist'] : false; + $db = isset($this->_server['db']) ? $this->_server['db'] : 0; - if (isset($this->_server['db']) && ($this->_server['db'] !== 0)) + $path .= '?persistent=' . (int) $persist . '&database=' . $db; + + if (!empty($this->_server['auth'])) { - $path = 'tcp://' . $path . '?database=' . $this->_server['db']; + $path .= '&auth=' . $this->_server['auth']; } - + ini_set('session.save_path', $path); ini_set('session.save_handler', 'redis'); } diff --git a/libraries/src/Cache/Storage/RedisStorage.php b/libraries/src/Cache/Storage/RedisStorage.php index febed02ab33f9..3347d116497d8 100644 --- a/libraries/src/Cache/Storage/RedisStorage.php +++ b/libraries/src/Cache/Storage/RedisStorage.php @@ -68,49 +68,47 @@ protected function getConnection() return false; } - $app = \JFactory::getApplication(); + $config = \JFactory::getConfig(); - $this->_persistent = $app->get('redis_persist', true); + $this->_persistent = $config->get('redis_persist', true); $server = array( - 'host' => $app->get('redis_server_host', 'localhost'), - 'port' => $app->get('redis_server_port', 6379), - 'auth' => $app->get('redis_server_auth', null), - 'db' => (int) $app->get('redis_server_db', null), + 'host' => $config->get('redis_server_host', 'localhost'), + 'port' => $config->get('redis_server_port', 6379), + 'auth' => $config->get('redis_server_auth', null), + 'db' => (int) $config->get('redis_server_db', null), ); + // If you are trying to connect to a socket file, ignore the supplied port + if ($server['host'][0] === '/') + { + $server['port'] = 0; + } + static::$_redis = new \Redis; - if ($this->_persistent) + try { - try + if ($this->_persistent) { $connection = static::$_redis->pconnect($server['host'], $server['port']); - $auth = (!empty($server['auth'])) ? static::$_redis->auth($server['auth']) : true; } - catch (\RedisException $e) + else { - Log::add($e->getMessage(), Log::DEBUG); + $connection = static::$_redis->connect($server['host'], $server['port']); } } - else + catch (\RedisException $e) { - try - { - $connection = static::$_redis->connect($server['host'], $server['port']); - $auth = (!empty($server['auth'])) ? static::$_redis->auth($server['auth']) : true; - } - catch (\RedisException $e) - { - Log::add($e->getMessage(), Log::DEBUG); - } + Log::add($e->getMessage(), Log::DEBUG); } if ($connection == false) { static::$_redis = null; - if ($app->isClient('administrator')) + // Because the application instance may not be available on cli script, use it only if needed + if (\JFactory::getApplication()->isClient('administrator')) { \JError::raiseWarning(500, 'Redis connection failed'); } @@ -118,9 +116,22 @@ protected function getConnection() return false; } - if ($auth == false) + try + { + $auth = $server['auth'] ? static::$_redis->auth($server['auth']) : true; + } + catch (\RedisException $e) + { + $auth = false; + Log::add($e->getMessage(), Log::DEBUG); + } + + if ($auth === false) { - if ($app->isClient('administrator')) + static::$_redis = null; + + // Because the application instance may not be available on cli script, use it only if needed + if (\JFactory::getApplication()->isClient('administrator')) { \JError::raiseWarning(500, 'Redis authentication failed'); } @@ -134,7 +145,8 @@ protected function getConnection() { static::$_redis = null; - if ($app->isClient('administrator')) + // Because the application instance may not be available on cli script, use it only if needed + if (\JFactory::getApplication()->isClient('administrator')) { \JError::raiseWarning(500, 'Redis failed to select database'); } @@ -150,7 +162,8 @@ protected function getConnection() { static::$_redis = null; - if ($app->isClient('administrator')) + // Because the application instance may not be available on cli script, use it only if needed + if (\JFactory::getApplication()->isClient('administrator')) { \JError::raiseWarning(500, 'Redis ping failed'); }