/**
* Create the Redis connection
*
* @return \Redis|boolean Redis connection object on success, boolean on failure
*
* @since 3.4
* @note As of 4.0 this method will throw a JCacheExceptionConnecting object on connection failure
*/
protected function getConnection()
{
if (static::isSupported() == false) {
return false;
}
$app = Factory::getApplication();
$this->_persistent = $app->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));
// 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();
try {
if ($this->_persistent) {
$connection = static::$_redis->pconnect($server['host'], $server['port']);
} else {
$connection = static::$_redis->connect($server['host'], $server['port']);
}
} catch (\RedisException $e) {
$connection = false;
Log::add($e->getMessage(), Log::DEBUG);
}
if ($connection == false) {
static::$_redis = null;
throw new CacheConnectingException('Redis connection failed', 500);
}
try {
$auth = $server['auth'] ? static::$_redis->auth($server['auth']) : true;
} catch (\RedisException $e) {
$auth = false;
Log::add($e->getMessage(), Log::DEBUG);
}
if ($auth === false) {
static::$_redis = null;
throw new CacheConnectingException('Redis authentication failed', 500);
}
$select = static::$_redis->select($server['db']);
if ($select == false) {
static::$_redis = null;
throw new CacheConnectingException('Redis failed to select database', 500);
}
try {
static::$_redis->ping();
} catch (\RedisException $e) {
static::$_redis = null;
throw new CacheConnectingException('Redis ping failed', 500);
}
return static::$_redis;
}