cache.php: add unix socket connection support to Redis cache

This commit is contained in:
Zankaria 2024-09-11 15:50:23 +02:00 committed by Zankaria
parent 696f084749
commit 87899a0988

View file

@ -10,7 +10,7 @@ class Cache {
private static $cache; private static $cache;
public static function init() { public static function init() {
global $config; global $config;
switch ($config['cache']['enabled']) { switch ($config['cache']['enabled']) {
case 'memcached': case 'memcached':
self::$cache = new Memcached(); self::$cache = new Memcached();
@ -18,7 +18,16 @@ class Cache {
break; break;
case 'redis': case 'redis':
self::$cache = new Redis(); self::$cache = new Redis();
self::$cache->connect($config['cache']['redis'][0], $config['cache']['redis'][1]);
$ret = explode(':', $config['cache']['redis'][0]);
if (count($ret) > 0) {
// Unix socket.
self::$cache->connect($ret[1]);
} else {
// IP + port.
self::$cache->connect($ret[0], $config['cache']['redis'][1]);
}
if ($config['cache']['redis'][2]) { if ($config['cache']['redis'][2]) {
self::$cache->auth($config['cache']['redis'][2]); self::$cache->auth($config['cache']['redis'][2]);
} }
@ -31,9 +40,9 @@ class Cache {
} }
public static function get($key) { public static function get($key) {
global $config, $debug; global $config, $debug;
$key = $config['cache']['prefix'] . $key; $key = $config['cache']['prefix'] . $key;
$data = false; $data = false;
switch ($config['cache']['enabled']) { switch ($config['cache']['enabled']) {
case 'memcached': case 'memcached':
@ -67,20 +76,20 @@ class Cache {
$data = json_decode(self::$cache->get($key), true); $data = json_decode(self::$cache->get($key), true);
break; break;
} }
if ($config['debug']) if ($config['debug'])
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)'); $debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)');
return $data; return $data;
} }
public static function set($key, $value, $expires = false) { public static function set($key, $value, $expires = false) {
global $config, $debug; global $config, $debug;
$key = $config['cache']['prefix'] . $key; $key = $config['cache']['prefix'] . $key;
if (!$expires) if (!$expires)
$expires = $config['cache']['timeout']; $expires = $config['cache']['timeout'];
switch ($config['cache']['enabled']) { switch ($config['cache']['enabled']) {
case 'memcached': case 'memcached':
if (!self::$cache) if (!self::$cache)
@ -107,15 +116,15 @@ class Cache {
self::$cache[$key] = $value; self::$cache[$key] = $value;
break; break;
} }
if ($config['debug']) if ($config['debug'])
$debug['cached'][] = $key . ' (set)'; $debug['cached'][] = $key . ' (set)';
} }
public static function delete($key) { public static function delete($key) {
global $config, $debug; global $config, $debug;
$key = $config['cache']['prefix'] . $key; $key = $config['cache']['prefix'] . $key;
switch ($config['cache']['enabled']) { switch ($config['cache']['enabled']) {
case 'memcached': case 'memcached':
if (!self::$cache) if (!self::$cache)
@ -142,13 +151,13 @@ class Cache {
unset(self::$cache[$key]); unset(self::$cache[$key]);
break; break;
} }
if ($config['debug']) if ($config['debug'])
$debug['cached'][] = $key . ' (deleted)'; $debug['cached'][] = $key . ' (deleted)';
} }
public static function flush() { public static function flush() {
global $config; global $config;
switch ($config['cache']['enabled']) { switch ($config['cache']['enabled']) {
case 'memcached': case 'memcached':
if (!self::$cache) if (!self::$cache)
@ -170,8 +179,7 @@ class Cache {
self::init(); self::init();
return self::$cache->flushDB(); return self::$cache->flushDB();
} }
return false; return false;
} }
} }