Source code of Leftypol imageboard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.1 KiB

<?php
12 years ago
12 years ago
/*
* Copyright (c) 2010-2013 Tinyboard Development Group
12 years ago
*/
defined('TINYBOARD') or exit;
12 years ago
12 years ago
class Cache {
private static $cache;
public static function init() {
global $config;
12 years ago
switch ($config['cache']['enabled']) {
12 years ago
case 'memcached':
self::$cache = new Memcached();
self::$cache->addServers($config['cache']['memcached']);
break;
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;
12 years ago
case 'php':
self::$cache = array();
12 years ago
break;
}
12 years ago
}
public static function get($key) {
global $config, $debug;
$key = $config['cache']['prefix'] . $key;
$data = false;
12 years ago
switch ($config['cache']['enabled']) {
12 years ago
case 'memcached':
12 years ago
if (!self::$cache)
12 years ago
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;
case 'redis':
if (!self::$cache)
self::init();
$data = json_decode(self::$cache->get($key), true);
break;
}
12 years ago
if ($config['debug'])
$debug['cached'][] = $key . ($data === false ? ' (miss)' : ' (hit)');
12 years ago
return $data;
}
public static function set($key, $value, $expires = false) {
global $config, $debug;
12 years ago
$key = $config['cache']['prefix'] . $key;
12 years ago
if (!$expires)
12 years ago
$expires = $config['cache']['timeout'];
12 years ago
switch ($config['cache']['enabled']) {
12 years ago
case 'memcached':
12 years ago
if (!self::$cache)
12 years ago
self::init();
self::$cache->set($key, $value, $expires);
break;
case 'redis':
if (!self::$cache)
self::init();
self::$cache->setex($key, $expires, json_encode($value));
break;
12 years ago
case 'apc':
apc_store($key, $value, $expires);
break;
case 'xcache':
xcache_set($key, $value, $expires);
break;
case 'php':
self::$cache[$key] = $value;
break;
}
if ($config['debug'])
$debug['cached'][] = $key . ' (set)';
12 years ago
}
public static function delete($key) {
global $config, $debug;
12 years ago
$key = $config['cache']['prefix'] . $key;
12 years ago
switch ($config['cache']['enabled']) {
12 years ago
case 'memcached':
case 'redis':
12 years ago
if (!self::$cache)
12 years ago
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;
}
if ($config['debug'])
$debug['cached'][] = $key . ' (deleted)';
12 years ago
}
public static function flush() {
global $config;
12 years ago
switch ($config['cache']['enabled']) {
12 years ago
case 'memcached':
12 years ago
if (!self::$cache)
12 years ago
self::init();
return self::$cache->flush();
case 'apc':
return apc_clear_cache('user');
case 'php':
11 years ago
self::$cache = array();
12 years ago
break;
case 'redis':
if (!self::$cache)
self::init();
return self::$cache->flushDB();
}
12 years ago
return false;
}
12 years ago
}