2011-10-07 07:51:19 +02:00
|
|
|
<?php
|
|
|
|
class Cache {
|
|
|
|
private static $cache;
|
|
|
|
public static function init() {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
switch($config['cache']['enabled']) {
|
|
|
|
case 'memcached':
|
|
|
|
self::$cache = new Memcached();
|
|
|
|
self::$cache->addServers($config['cache']['memcached']);
|
|
|
|
break;
|
2011-10-11 12:49:14 +02:00
|
|
|
case 'php':
|
|
|
|
self::$cache = Array();
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function get($key) {
|
|
|
|
global $config, $debug;
|
|
|
|
|
2012-02-14 13:36:08 +01:00
|
|
|
$key = $config['cache']['prefix'] . $key;
|
|
|
|
|
2011-10-07 07:51:19 +02:00
|
|
|
$data = false;
|
|
|
|
switch($config['cache']['enabled']) {
|
|
|
|
case 'memcached':
|
2011-10-08 19:35:50 +02:00
|
|
|
if(!self::$cache)
|
|
|
|
self::init();
|
2011-10-07 07:51:19 +02:00
|
|
|
$data = self::$cache->get($key);
|
|
|
|
break;
|
|
|
|
case 'apc':
|
|
|
|
$data = apc_fetch($key);
|
|
|
|
break;
|
|
|
|
case 'xcache':
|
|
|
|
$data = xcache_get($key);
|
|
|
|
break;
|
2011-10-11 12:49:14 +02:00
|
|
|
case 'php':
|
|
|
|
$data = isset(self::$cache[$key]) ? self::$cache[$key] : false;
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// debug
|
|
|
|
if($data && $config['debug']) {
|
|
|
|
$debug['cached'][] = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
public static function set($key, $value, $expires = false) {
|
|
|
|
global $config;
|
|
|
|
|
2012-02-14 13:36:08 +01:00
|
|
|
$key = $config['cache']['prefix'] . $key;
|
|
|
|
|
2011-10-07 07:51:19 +02:00
|
|
|
if(!$expires)
|
|
|
|
$expires = $config['cache']['timeout'];
|
|
|
|
|
|
|
|
switch($config['cache']['enabled']) {
|
|
|
|
case 'memcached':
|
2011-10-08 19:35:50 +02:00
|
|
|
if(!self::$cache)
|
|
|
|
self::init();
|
2011-10-07 07:51:19 +02:00
|
|
|
self::$cache->set($key, $value, $expires);
|
|
|
|
break;
|
|
|
|
case 'apc':
|
|
|
|
apc_store($key, $value, $expires);
|
|
|
|
break;
|
|
|
|
case 'xcache':
|
|
|
|
xcache_set($key, $value, $expires);
|
|
|
|
break;
|
2011-10-11 12:49:14 +02:00
|
|
|
case 'php':
|
|
|
|
self::$cache[$key] = $value;
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function delete($key) {
|
|
|
|
global $config;
|
|
|
|
|
2012-02-14 13:36:08 +01:00
|
|
|
$key = $config['cache']['prefix'] . $key;
|
|
|
|
|
2011-10-07 07:51:19 +02:00
|
|
|
switch($config['cache']['enabled']) {
|
|
|
|
case 'memcached':
|
2011-10-08 19:35:50 +02:00
|
|
|
if(!self::$cache)
|
|
|
|
self::init();
|
2011-10-07 07:51:19 +02:00
|
|
|
self::$cache->delete($key);
|
|
|
|
break;
|
|
|
|
case 'apc':
|
|
|
|
apc_delete($key);
|
|
|
|
break;
|
|
|
|
case 'xcache':
|
|
|
|
xcache_unset($key);
|
|
|
|
break;
|
2011-10-11 12:49:14 +02:00
|
|
|
case 'php':
|
|
|
|
unset(self::$cache[$key]);
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function flush() {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
switch($config['cache']['enabled']) {
|
|
|
|
case 'memcached':
|
2011-10-08 19:35:50 +02:00
|
|
|
if(!self::$cache)
|
|
|
|
self::init();
|
2011-10-07 07:51:19 +02:00
|
|
|
return self::$cache->flush();
|
|
|
|
case 'apc':
|
2011-10-08 19:35:50 +02:00
|
|
|
return apc_clear_cache('user');
|
2011-10-11 12:49:14 +02:00
|
|
|
case 'php':
|
|
|
|
self::$cache[$key] = Array();
|
|
|
|
break;
|
2011-10-07 07:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|