Browse Source

memcached, APC, xcache

pull/40/head
Savetheinternet 11 years ago
parent
commit
555688e880
  1. 95
      inc/cache.php
  2. 22
      inc/config.php
  3. 17
      inc/display.php
  4. 47
      inc/functions.php
  5. 8
      mod.php

95
inc/cache.php

@ -0,0 +1,95 @@
<?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;
}
}
public static function get($key) {
global $config, $debug;
if(!self::$cache)
self::init();
$data = false;
switch($config['cache']['enabled']) {
case 'memcached':
$data = self::$cache->get($key);
break;
case 'apc':
$data = apc_fetch($key);
break;
case 'xcache':
$data = xcache_get($key);
break;
}
// debug
if($data && $config['debug']) {
$debug['cached'][] = $key;
}
return $data;
}
public static function set($key, $value, $expires = false) {
global $config;
if(!self::$cache)
self::init();
if(!$expires)
$expires = $config['cache']['timeout'];
switch($config['cache']['enabled']) {
case 'memcached':
self::$cache->set($key, $value, $expires);
break;
case 'apc':
apc_store($key, $value, $expires);
break;
case 'xcache':
xcache_set($key, $value, $expires);
break;
}
}
public static function delete($key) {
global $config;
if(!self::$cache)
self::init();
switch($config['cache']['enabled']) {
case 'memcached':
self::$cache->delete($key);
break;
case 'apc':
apc_delete($key);
break;
case 'xcache':
xcache_unset($key);
break;
}
}
public static function flush() {
global $config;
if(!self::$cache)
self::init();
switch($config['cache']['enabled']) {
case 'memcached':
return self::$cache->flush();
case 'apc':
return apc_clear_cache();
}
return false;
}
}

22
inc/config.php

@ -23,7 +23,7 @@
$config = Array(
'db' => Array(),
'memcached' => Array(),
'cache' => Array(),
'cookies' => Array(),
'error' => Array(),
'dir' => Array(),
@ -98,14 +98,24 @@
// Timeout duration in seconds (not all drivers support this)
$config['db']['timeout'] = 5;
// Optional Memcached server for more cache/optimization (currently at debug state)
$config['memcached']['enabled'] = false;
/*
* ====================
* Cache settings
* ====================
*/
$config['cache']['enabled'] = false;
// $config['cache']['enabled'] = 'memcached';
// $config['cache']['enabled'] = 'apc';
// $config['cache']['enabled'] = 'xcache';
// Timeout for cached objects such as posts and HTML
$config['cache']['timeout'] = 43200; // 12 hours
// Memcached servers to use - http://www.php.net/manual/en/memcached.addservers.php
$config['memcached']['servers'] = Array(
$config['cache']['memcached'] = Array(
Array('localhost', 11211)
);
// Timeout for cached objects such as posts and HTML
$config['memcached']['timeout'] = 43200; // 12 hours
/*
* ====================

17
inc/display.php

@ -372,26 +372,17 @@
}
public function build($index=false) {
global $board, $config, $memcached, $debug;
if(!$this->mod && $config['memcached']['enabled']) {
if($built = $memcached->get($this->memcached_key($index))) {
if($config['debug']) {
$debug['memcached'][] = $this->memcached_key($index);
}
return $built;
}
}
global $board, $config, $debug;
$built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
if(!$this->mod && $config['memcached']['enabled']) {
$memcached->set($this->memcached_key($index), $built, time() + $config['memcached']['timeout']);
if(!$this->mod && $index && $config['cache']['enabled']) {
cache::set($this->cache_key($index), $built);
}
return $built;
}
function memcached_key($index) {
function cache_key($index) {
global $board;
return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id;

47
inc/functions.php

@ -21,7 +21,7 @@
if($config['debug']) {
if(!isset($debug)) {
$debug = Array('sql' => Array(), 'purge' => Array(), 'memcached' => Array());
$debug = Array('sql' => Array(), 'purge' => Array(), 'cached' => Array());
$debug['start'] = microtime(true);
}
}
@ -102,8 +102,8 @@
if($config['recaptcha'])
require_once 'inc/contrib/recaptcha/recaptchalib.php';
if($config['memcached']['enabled'])
memcached_open();
if($config['cache']['enabled'])
require_once 'inc/cache.php';
}
function basic_error_function_because_the_other_isnt_loaded_yet($message) {
@ -131,15 +131,6 @@
}
}
// Memcached
function memcached_open() {
global $memcached, $config;
if($memcached) return;
$memcached = new Memcached();
$memcached->addServers($config['memcached']['servers']);
}
function loadThemeConfig($_theme) {
global $config;
@ -478,7 +469,7 @@
}
function checkBan($board = 0) {
global $config, $memcached;
global $config;
if(!isset($_SERVER['REMOTE_ADDR'])) {
// Server misconfiguration
@ -718,7 +709,7 @@
}
function index($page, $mod=false) {
global $board, $config, $memcached, $debug;
global $board, $config, $debug;
$body = '';
$offset = round($page*$config['threads_per_page']-$config['threads_per_page']);
@ -730,12 +721,8 @@
if($query->rowcount() < 1 && $page > 1) return false;
while($th = $query->fetch()) {
if(!$mod && $config['memcached']['enabled']) {
if($built = $memcached->get("thread_index_{$board['uri']}_{$th['id']}")) {
if($config['debug']) {
$debug['memcached'][] = "thread_index_{$board['uri']}_{$th['id']}";
}
if(!$mod && $config['cache']['enabled']) {
if($built = cache::get("thread_index_{$board['uri']}_{$th['id']}")) {
$body .= '<div id="thread_' . $th['id'] . '">' . $built . '</div>';
continue;
}
@ -916,11 +903,11 @@
}
function checkMute() {
global $config, $memcached;
global $config, $debug;
if($config['memcached']['enabled']) {
if($config['cache']['enabled']) {
// Cached mute?
if(($mute = $memcached->get("mute_${_SERVER['REMOTE_ADDR']}")) && ($mutetime = $memcached->get("mutetime_${_SERVER['REMOTE_ADDR']}"))) {
if(($mute = cache::get("mute_${_SERVER['REMOTE_ADDR']}")) && ($mutetime = cache::get("mutetime_${_SERVER['REMOTE_ADDR']}"))) {
error(sprintf($config['error']['youaremuted'], $mute['time'] + $mutetime - time()));
}
}
@ -938,9 +925,9 @@
}
if($mute['time'] + $mutetime > time()) {
if($config['memcached']['enabled']) {
$memcached->set("mute_${_SERVER['REMOTE_ADDR']}", $mute, $mute['time'] + $mutetime);
$memcached->set("mutetime_${_SERVER['REMOTE_ADDR']}", $mutetime, $mute['time'] + $mutetime);
if($config['cache']['enabled']) {
cache::set("mute_${_SERVER['REMOTE_ADDR']}", $mute, $mute['time'] + $mutetime);
cache::set("mutetime_${_SERVER['REMOTE_ADDR']}", $mutetime, $mute['time'] + $mutetime);
}
// Not expired yet
error(sprintf($config['error']['youaremuted'], $mute['time'] + $mutetime - time()));
@ -1362,13 +1349,13 @@
}
function buildThread($id, $return=false, $mod=false) {
global $board, $config, $memcached;
global $board, $config;
$id = round($id);
if($config['memcached']['enabled'] && !$mod) {
if($config['cache']['enabled'] && !$mod) {
// Clear cache
$memcached->delete("thread_index_{$board['uri']}_{$id}");
$memcached->delete("thread_{$board['uri']}_{$id}");
cache::delete("thread_index_{$board['uri']}_{$id}");
cache::delete("thread_{$board['uri']}_{$id}");
}
$query = prepare(sprintf("SELECT * FROM `posts_%s` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri']));

8
mod.php

@ -151,7 +151,7 @@
if(hasPermission($config['mod']['rebuild'])) {
$fieldset['Administration'] .= '<li><a href="?/rebuild">Rebuild static files</a></li>';
}
if(hasPermission($config['mod']['rebuild']) && $config['memcached']['enabled']) {
if(hasPermission($config['mod']['rebuild']) && $config['cache']['enabled']) {
$fieldset['Administration'] .= '<li><a href="?/flush">Clear cache</a></li>';
}
if(hasPermission($config['mod']['show_config'])) {
@ -1588,13 +1588,13 @@
);
} elseif(preg_match('/^\/flush$/', $query)) {
if(!hasPermission($config['mod']['rebuild'])) error($config['error']['noaccess']);
if(!$config['memcached']['enabled']) error('Memcached is not enabled.');
if(!$config['cache']['enabled']) error('Cache is not enabled.');
if($memcached->flush()) {
if(cache::flush()) {
$body = 'Successfully invalidated all items in the cache.';
modLog('Cleared cache');
} else {
$body = $memcached->getResultMessage();
$body = 'An error occured while trying to flush cache.';
}
echo Element('page.html', Array(

Loading…
Cancel
Save