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
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2012 Tinyboard Development Group
|
|
|
|
*/
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
|
2012-04-11 18:49:22 +02:00
|
|
|
// You cannot request this file directly.
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2012-04-10 17:18:38 +02:00
|
|
|
class Cache {
|
|
|
|
private static $cache;
|
|
|
|
public static function init() {
|
|
|
|
global $config;
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
$key = $config['cache']['prefix'] . $key;
|
|
|
|
|
|
|
|
$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;
|
|
|
|
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;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2012-04-10 17:18:38 +02:00
|
|
|
|
|
|
|
// debug
|
2012-05-20 11:06:27 +02:00
|
|
|
if ($data !== false && $config['debug']) {
|
2012-04-10 17:18:38 +02:00
|
|
|
$debug['cached'][] = $key;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2012-04-10 17:18:38 +02:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
public static function set($key, $value, $expires = false) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$key = $config['cache']['prefix'] . $key;
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!$expires)
|
2012-04-10 17:18:38 +02:00
|
|
|
$expires = $config['cache']['timeout'];
|
|
|
|
|
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;
|
|
|
|
case 'apc':
|
|
|
|
apc_store($key, $value, $expires);
|
|
|
|
break;
|
|
|
|
case 'xcache':
|
|
|
|
xcache_set($key, $value, $expires);
|
|
|
|
break;
|
|
|
|
case 'php':
|
|
|
|
self::$cache[$key] = $value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function delete($key) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$key = $config['cache']['prefix'] . $key;
|
|
|
|
|
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;
|
|
|
|
case 'apc':
|
|
|
|
apc_delete($key);
|
|
|
|
break;
|
|
|
|
case 'xcache':
|
|
|
|
xcache_unset($key);
|
|
|
|
break;
|
|
|
|
case 'php':
|
|
|
|
unset(self::$cache[$key]);
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2012-04-10 17:18:38 +02:00
|
|
|
}
|
|
|
|
public static function flush() {
|
|
|
|
global $config;
|
|
|
|
|
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();
|
|
|
|
case 'apc':
|
|
|
|
return apc_clear_cache('user');
|
|
|
|
case 'php':
|
2012-08-27 13:50:15 +02:00
|
|
|
self::$cache[$key] = 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
|
|
|
|
|
|
|
return false;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
2012-04-10 17:18:38 +02:00
|
|
|
}
|
|
|
|
|