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