cache.php: wrap new cache drivers

This commit is contained in:
Zankaria 2024-04-26 13:55:58 +02:00 committed by Zankaria
parent 33f83af1b1
commit 842b4fdcee

View file

@ -4,183 +4,89 @@
* Copyright (c) 2010-2013 Tinyboard Development Group * Copyright (c) 2010-2013 Tinyboard Development Group
*/ */
use Vichan\Data\Driver\{CacheDriver, ApcuCacheDriver, ArrayCacheDriver, FsCacheDriver, MemcachedCacheDriver, NoneCacheDriver, RedisCacheDriver};
defined('TINYBOARD') or exit; defined('TINYBOARD') or exit;
class Cache { class Cache {
private static $cache; private static function buildCache(): CacheDriver {
public static function init() {
global $config; global $config;
switch ($config['cache']['enabled']) { switch ($config['cache']['enabled']) {
case 'memcached': case 'memcached':
self::$cache = new Memcached(); return new MemcachedCacheDriver(
self::$cache->addServers($config['cache']['memcached']); $config['cache']['prefix'],
break; $config['cache']['memcached']
);
case 'redis': case 'redis':
self::$cache = new Redis(); return new RedisCacheDriver(
$config['cache']['prefix'],
$ret = explode(':', $config['cache']['redis'][0]); $config['cache']['redis'][0],
if (count($ret) > 0) { $config['cache']['redis'][1],
// Unix socket. $config['cache']['redis'][2],
self::$cache->connect($ret[1]); $config['cache']['redis'][3]
} else { );
// IP + port. case 'apcu':
self::$cache->connect($ret[0], $config['cache']['redis'][1]); return new ApcuCacheDriver;
} case 'fs':
return new FsCacheDriver(
if ($config['cache']['redis'][2]) { $config['cache']['prefix'],
self::$cache->auth($config['cache']['redis'][2]); "tmp/cache/{$config['cache']['prefix']}",
} '.lock',
self::$cache->select($config['cache']['redis'][3]) or die('cache select failure'); $config['auto_maintenance'] ? 1000 : false
break; );
case 'none':
return new NoneCacheDriver();
case 'php': case 'php':
self::$cache = array(); default:
break; return new ArrayCacheDriver();
} }
} }
public static function getCache(): CacheDriver {
static $cache;
return $cache ??= self::buildCache();
}
public static function get($key) { public static function get($key) {
global $config, $debug; global $config, $debug;
$key = $config['cache']['prefix'] . $key; $ret = self::getCache()->get($key);
if ($ret === null) {
$data = false; $ret = false;
switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
self::init();
$data = self::$cache->get($key);
break;
case 'apc':
$data = apc_fetch($key);
break;
case 'xcache':
$data = xcache_get($key);
break;
case 'php':
$data = isset(self::$cache[$key]) ? self::$cache[$key] : false;
break;
case 'fs':
$key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key);
if (!file_exists('tmp/cache/'.$key)) {
$data = false;
}
else {
$data = file_get_contents('tmp/cache/'.$key);
$data = json_decode($data, true);
}
break;
case 'redis':
if (!self::$cache)
self::init();
$ret = self::$cache->get($key);
$data = $ret !== false ? json_decode($ret, true) : false;
break;
} }
if ($config['debug']) if ($config['debug']) {
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)'); $debug['cached'][] = $config['cache']['prefix'] . $key . ($ret === false ? ' (miss)' : ' (hit)');
}
return $data; return $ret;
} }
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; if (!$expires) {
if (!$expires)
$expires = $config['cache']['timeout']; $expires = $config['cache']['timeout'];
switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
self::init();
self::$cache->set($key, $value, $expires);
break;
case 'redis':
if (!self::$cache)
self::init();
self::$cache->setEx($key, $expires, json_encode($value));
break;
case 'apc':
apc_store($key, $value, $expires);
break;
case 'xcache':
xcache_set($key, $value, $expires);
break;
case 'fs':
$key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key);
file_put_contents('tmp/cache/'.$key, json_encode($value));
break;
case 'php':
self::$cache[$key] = $value;
break;
} }
if ($config['debug']) self::getCache()->set($key, $value, $expires);
$debug['cached'][] = $key . ' (set)';
if ($config['debug']) {
$debug['cached'][] = $config['cache']['prefix'] . $key . ' (set)';
}
} }
public static function delete($key) { public static function delete($key) {
global $config, $debug; global $config, $debug;
$key = $config['cache']['prefix'] . $key; self::getCache()->delete($key);
switch ($config['cache']['enabled']) { if ($config['debug']) {
case 'memcached': $debug['cached'][] = $config['cache']['prefix'] . $key . ' (deleted)';
if (!self::$cache)
self::init();
self::$cache->delete($key);
break;
case 'redis':
if (!self::$cache)
self::init();
self::$cache->del($key);
break;
case 'apc':
apc_delete($key);
break;
case 'xcache':
xcache_unset($key);
break;
case 'fs':
$key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key);
@unlink('tmp/cache/'.$key);
break;
case 'php':
unset(self::$cache[$key]);
break;
} }
if ($config['debug'])
$debug['cached'][] = $key . ' (deleted)';
} }
public static function flush() { public static function flush() {
global $config; self::getCache()->flush();
switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
self::init();
return self::$cache->flush();
case 'apc':
return apc_clear_cache('user');
case 'php':
self::$cache = array();
break;
case 'fs':
$files = glob('tmp/cache/*');
foreach ($files as $file) {
unlink($file);
}
break;
case 'redis':
if (!self::$cache)
self::init();
return self::$cache->flushDB();
}
return false; return false;
} }
} }