Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch redis unavailable error and fallback to default cache #2706

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions core/class/cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,60 @@ public static function stats($_details = false) {
* @name getCache()
* @access public
* @static
* @param string $_engine to override the current cache defined in configuration
* @return \Doctrine\Common\Cache\CacheProvider
*/
public static function getCache() {
if (self::$cache !== null) {
public static function getCache($_engine = null) {
if ($_engine === null && self::$cache !== null) {
return self::$cache;
}
$engine = config::byKey('cache::engine');
if ($engine == 'MemcachedCache' && !class_exists('memcached')) {
$engine = 'FilesystemCache';
config::save('cache::engine', 'FilesystemCache');
}
if ($engine == 'RedisCache' && !class_exists('redis')) {
$engine = 'FilesystemCache';
config::save('cache::engine', 'FilesystemCache');
if( $_engine === null){
// get current cache
$engine = config::byKey('cache::engine');
}else{
// override existing configuration
$engine = $_engine;
config::save('cache::engine', $_engine);
}
switch ($engine) {
case 'FilesystemCache':
self::$cache = new \Doctrine\Common\Cache\FilesystemCache(self::getFolder());
break;
case 'PhpFileCache':
self::$cache = new \Doctrine\Common\Cache\FilesystemCache(self::getFolder());
break;
case 'MemcachedCache':
// check if memcached extention is available
if (!class_exists('memcached')) {
log::add( __CLASS__, 'error', 'memcached extension not installed, fall back to FilesystemCache.');
return self::getCache( 'FilesystemCache');
}
$memcached = new Memcached();
$memcached->addServer(config::byKey('cache::memcacheaddr'), config::byKey('cache::memcacheport'));
self::$cache = new \Doctrine\Common\Cache\MemcachedCache();
self::$cache->setMemcached($memcached);
break;
case 'RedisCache':
// check if redis extension is available
if (!class_exists('redis')) {
log::add( __CLASS__, 'error', 'redis extension not installed, fall back to FilesystemCache.');
return self::getCache( 'FilesystemCache');
}
$redis = new Redis();
$redisAddr = config::byKey('cache::redisaddr');
if (strncmp($redisAddr, '/', 1) === 0) {
$redis->connect($redisAddr);
} else {
$redis->connect($redisAddr, config::byKey('cache::redisport'));
try{
// try to connect to redis
if (strncmp($redisAddr, '/', 1) === 0) {
$redis->connect($redisAddr);
} else {
$redis->connect($redisAddr, config::byKey('cache::redisport'));
}
}catch( Exception $e){
// error : fall back to FilesystemCache
log::add( __CLASS__, 'error', 'Unable to connect to redis instance, fall back to FilesystemCache.'."\n".$e->getMessage());
return self::getCache( 'FilesystemCache');
}
self::$cache = new \Doctrine\Common\Cache\RedisCache();
self::$cache->setRedis($redis);
break;
default:
default: // default is FilesystemCache
self::$cache = new \Doctrine\Common\Cache\FilesystemCache(self::getFolder());
break;
}
Expand Down
Loading