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
|
|
|
*/
|
|
|
|
|
2013-09-06 12:12:04 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2012-04-11 18:49:22 +02:00
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
class Cache {
|
|
|
|
private static $cache;
|
|
|
|
public static function init() {
|
|
|
|
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':
|
|
|
|
self::$cache = new Memcached();
|
|
|
|
self::$cache->addServers($config['cache']['memcached']);
|
|
|
|
break;
|
2012-12-03 03:00:58 +01:00
|
|
|
case 'redis':
|
|
|
|
self::$cache = new Redis();
|
|
|
|
self::$cache->connect($config['cache']['redis'][0], $config['cache']['redis'][1]);
|
|
|
|
if ($config['cache']['redis'][2]) {
|
|
|
|
self::$cache->auth($config['cache']['redis'][2]);
|
|
|
|
}
|
|
|
|
self::$cache->select($config['cache']['redis'][3]) or die('cache select failure');
|
|
|
|
break;
|
2012-04-10 17:18:38 +02:00
|
|
|
case 'php':
|
2012-08-27 13:50:15 +02:00
|
|
|
self::$cache = array();
|
2012-04-10 17:18:38 +02:00
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2012-04-10 17:18:38 +02:00
|
|
|
}
|
|
|
|
public static function get($key) {
|
|
|
|
global $config, $debug;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
$key = $config['cache']['prefix'] . $key;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
$data = false;
|
2012-04-12 16:18:19 +02:00
|
|
|
switch ($config['cache']['enabled']) {
|
2012-04-10 17:18:38 +02:00
|
|
|
case 'memcached':
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!self::$cache)
|
2012-04-10 17:18:38 +02:00
|
|
|
self::init();
|
|
|
|
$data = self::$cache->get($key);
|
|
|
|
break;
|
2022-08-29 17:45:59 +02:00
|
|
|
case 'apcu':
|
|
|
|
$data = apcu_fetch($key);
|
|
|
|
break;
|
2012-04-10 17:18:38 +02:00
|
|
|
case 'php':
|
|
|
|
$data = isset(self::$cache[$key]) ? self::$cache[$key] : false;
|
|
|
|
break;
|
2015-04-05 17:13:55 +02:00
|
|
|
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;
|
2012-12-03 03:00:58 +01:00
|
|
|
case 'redis':
|
|
|
|
if (!self::$cache)
|
|
|
|
self::init();
|
|
|
|
$data = json_decode(self::$cache->get($key), true);
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2013-08-29 00:07:27 +02:00
|
|
|
if ($config['debug'])
|
|
|
|
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)');
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
$key = $config['cache']['prefix'] . $key;
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!$expires)
|
2012-04-10 17:18:38 +02:00
|
|
|
$expires = $config['cache']['timeout'];
|
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':
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!self::$cache)
|
2012-04-10 17:18:38 +02:00
|
|
|
self::init();
|
|
|
|
self::$cache->set($key, $value, $expires);
|
|
|
|
break;
|
2012-12-03 03:00:58 +01:00
|
|
|
case 'redis':
|
|
|
|
if (!self::$cache)
|
|
|
|
self::init();
|
|
|
|
self::$cache->setex($key, $expires, json_encode($value));
|
|
|
|
break;
|
2022-08-29 17:45:59 +02:00
|
|
|
case 'apcu':
|
|
|
|
apcu_store($key, $value, $expires);
|
|
|
|
break;
|
2015-04-05 17:13:55 +02:00
|
|
|
case 'fs':
|
|
|
|
$key = str_replace('/', '::', $key);
|
|
|
|
$key = str_replace("\0", '', $key);
|
|
|
|
file_put_contents('tmp/cache/'.$key, json_encode($value));
|
|
|
|
break;
|
2012-04-10 17:18:38 +02:00
|
|
|
case 'php':
|
|
|
|
self::$cache[$key] = $value;
|
|
|
|
break;
|
2013-08-29 00:07:27 +02:00
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2013-08-29 00:07:27 +02:00
|
|
|
if ($config['debug'])
|
|
|
|
$debug['cached'][] = $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
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
$key = $config['cache']['prefix'] . $key;
|
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':
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!self::$cache)
|
2012-04-10 17:18:38 +02:00
|
|
|
self::init();
|
|
|
|
self::$cache->delete($key);
|
|
|
|
break;
|
2022-01-17 05:27:08 +01:00
|
|
|
case 'redis':
|
|
|
|
if (!self::$cache)
|
|
|
|
self::init();
|
|
|
|
self::$cache->del($key);
|
|
|
|
break;
|
2022-08-29 17:45:59 +02:00
|
|
|
case 'apcu':
|
|
|
|
apcu_delete($key);
|
|
|
|
break;
|
2015-04-05 17:13:55 +02:00
|
|
|
case 'fs':
|
|
|
|
$key = str_replace('/', '::', $key);
|
|
|
|
$key = str_replace("\0", '', $key);
|
2015-04-06 22:51:02 +02:00
|
|
|
@unlink('tmp/cache/'.$key);
|
2015-04-05 17:13:55 +02:00
|
|
|
break;
|
2012-04-10 17:18:38 +02:00
|
|
|
case 'php':
|
|
|
|
unset(self::$cache[$key]);
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
2013-08-29 00:07:27 +02:00
|
|
|
if ($config['debug'])
|
|
|
|
$debug['cached'][] = $key . ' (deleted)';
|
2012-04-10 17:18:38 +02:00
|
|
|
}
|
|
|
|
public static function flush() {
|
|
|
|
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':
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!self::$cache)
|
2012-04-10 17:18:38 +02:00
|
|
|
self::init();
|
|
|
|
return self::$cache->flush();
|
2022-08-29 17:45:59 +02:00
|
|
|
case 'apcu':
|
|
|
|
return apcu_clear_cache('user');
|
2012-04-10 17:18:38 +02:00
|
|
|
case 'php':
|
2013-09-06 19:09:52 +02:00
|
|
|
self::$cache = array();
|
2012-04-10 17:18:38 +02:00
|
|
|
break;
|
2015-04-05 17:13:55 +02:00
|
|
|
case 'fs':
|
|
|
|
$files = glob('tmp/cache/*');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
break;
|
2012-12-03 03:00:58 +01:00
|
|
|
case 'redis':
|
|
|
|
if (!self::$cache)
|
|
|
|
self::init();
|
|
|
|
return self::$cache->flushDB();
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2024-03-07 12:47:14 +01:00
|
|
|
|
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
|
|
|
}
|