2011-10-07 16:51:19 +11:00
|
|
|
<?php
|
2012-04-11 01:18:38 +10:00
|
|
|
|
2012-04-12 02:49:22 +10:00
|
|
|
/*
|
2013-01-20 21:23:46 +11:00
|
|
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
2012-04-12 02:49:22 +10:00
|
|
|
*/
|
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
use Vichan\Data\Driver\{CacheDriver, ApcuCacheDriver, ArrayCacheDriver, FsCacheDriver, MemcachedCacheDriver, NoneCacheDriver, RedisCacheDriver};
|
|
|
|
|
2013-09-06 20:12:04 +10:00
|
|
|
defined('TINYBOARD') or exit;
|
2012-04-12 02:49:22 +10:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
|
2012-04-11 01:18:38 +10:00
|
|
|
class Cache {
|
2024-04-26 13:55:58 +02:00
|
|
|
private static function buildCache(): CacheDriver {
|
2012-04-11 01:18:38 +10:00
|
|
|
global $config;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2012-04-13 00:18:19 +10:00
|
|
|
switch ($config['cache']['enabled']) {
|
2012-04-11 01:18:38 +10:00
|
|
|
case 'memcached':
|
2024-04-26 13:55:58 +02:00
|
|
|
return new MemcachedCacheDriver(
|
|
|
|
$config['cache']['prefix'],
|
|
|
|
$config['cache']['memcached']
|
|
|
|
);
|
2012-12-02 19:00:58 -07:00
|
|
|
case 'redis':
|
2024-04-26 13:55:58 +02:00
|
|
|
return new RedisCacheDriver(
|
|
|
|
$config['cache']['prefix'],
|
|
|
|
$config['cache']['redis'][0],
|
|
|
|
$config['cache']['redis'][1],
|
|
|
|
$config['cache']['redis'][2],
|
|
|
|
$config['cache']['redis'][3]
|
|
|
|
);
|
|
|
|
case 'apcu':
|
|
|
|
return new ApcuCacheDriver;
|
|
|
|
case 'fs':
|
|
|
|
return new FsCacheDriver(
|
|
|
|
$config['cache']['prefix'],
|
|
|
|
"tmp/cache/{$config['cache']['prefix']}",
|
|
|
|
'.lock',
|
|
|
|
$config['auto_maintenance'] ? 1000 : false
|
|
|
|
);
|
|
|
|
case 'none':
|
|
|
|
return new NoneCacheDriver();
|
2012-04-11 01:18:38 +10:00
|
|
|
case 'php':
|
2024-04-26 13:55:58 +02:00
|
|
|
default:
|
|
|
|
return new ArrayCacheDriver();
|
2011-10-07 16:51:19 +11:00
|
|
|
}
|
2012-04-11 01:18:38 +10:00
|
|
|
}
|
2024-04-26 13:55:58 +02:00
|
|
|
|
|
|
|
public static function getCache(): CacheDriver {
|
|
|
|
static $cache;
|
|
|
|
return $cache ??= self::buildCache();
|
|
|
|
}
|
|
|
|
|
2012-04-11 01:18:38 +10:00
|
|
|
public static function get($key) {
|
|
|
|
global $config, $debug;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
$ret = self::getCache()->get($key);
|
|
|
|
if ($ret === null) {
|
|
|
|
$ret = false;
|
2011-10-07 16:51:19 +11:00
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
if ($config['debug']) {
|
|
|
|
$debug['cached'][] = $config['cache']['prefix'] . $key . ($ret === false ? ' (miss)' : ' (hit)');
|
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
return $ret;
|
2012-04-11 01:18:38 +10:00
|
|
|
}
|
|
|
|
public static function set($key, $value, $expires = false) {
|
2013-08-30 08:39:11 +10:00
|
|
|
global $config, $debug;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
if (!$expires) {
|
2012-04-11 01:18:38 +10:00
|
|
|
$expires = $config['cache']['timeout'];
|
2013-08-29 08:07:27 +10:00
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
self::getCache()->set($key, $value, $expires);
|
|
|
|
|
|
|
|
if ($config['debug']) {
|
|
|
|
$debug['cached'][] = $config['cache']['prefix'] . $key . ' (set)';
|
|
|
|
}
|
2012-04-11 01:18:38 +10:00
|
|
|
}
|
|
|
|
public static function delete($key) {
|
2013-08-29 08:07:27 +10:00
|
|
|
global $config, $debug;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
self::getCache()->delete($key);
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2024-04-26 13:55:58 +02:00
|
|
|
if ($config['debug']) {
|
|
|
|
$debug['cached'][] = $config['cache']['prefix'] . $key . ' (deleted)';
|
2011-10-07 16:51:19 +11:00
|
|
|
}
|
2012-04-11 01:18:38 +10:00
|
|
|
}
|
|
|
|
public static function flush() {
|
2024-04-26 13:55:58 +02:00
|
|
|
self::getCache()->flush();
|
2012-04-11 01:18:38 +10:00
|
|
|
return false;
|
2011-10-07 16:51:19 +11:00
|
|
|
}
|
2012-04-11 01:18:38 +10:00
|
|
|
}
|