Browse Source

memcached, APC, xcache

pull/40/head
Savetheinternet 13 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( $config = Array(
'db' => Array(), 'db' => Array(),
'memcached' => Array(), 'cache' => Array(),
'cookies' => Array(), 'cookies' => Array(),
'error' => Array(), 'error' => Array(),
'dir' => Array(), 'dir' => Array(),
@ -98,14 +98,24 @@
// Timeout duration in seconds (not all drivers support this) // Timeout duration in seconds (not all drivers support this)
$config['db']['timeout'] = 5; $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 // Memcached servers to use - http://www.php.net/manual/en/memcached.addservers.php
$config['memcached']['servers'] = Array( $config['cache']['memcached'] = Array(
Array('localhost', 11211) 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) { public function build($index=false) {
global $board, $config, $memcached, $debug; global $board, $config, $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;
}
}
$built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index)); $built = Element('post_thread.html', Array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
if(!$this->mod && $config['memcached']['enabled']) { if(!$this->mod && $index && $config['cache']['enabled']) {
$memcached->set($this->memcached_key($index), $built, time() + $config['memcached']['timeout']); cache::set($this->cache_key($index), $built);
} }
return $built; return $built;
} }
function memcached_key($index) { function cache_key($index) {
global $board; global $board;
return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id; return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id;

47
inc/functions.php

@ -21,7 +21,7 @@
if($config['debug']) { if($config['debug']) {
if(!isset($debug)) { if(!isset($debug)) {
$debug = Array('sql' => Array(), 'purge' => Array(), 'memcached' => Array()); $debug = Array('sql' => Array(), 'purge' => Array(), 'cached' => Array());
$debug['start'] = microtime(true); $debug['start'] = microtime(true);
} }
} }
@ -102,8 +102,8 @@
if($config['recaptcha']) if($config['recaptcha'])
require_once 'inc/contrib/recaptcha/recaptchalib.php'; require_once 'inc/contrib/recaptcha/recaptchalib.php';
if($config['memcached']['enabled']) if($config['cache']['enabled'])
memcached_open(); require_once 'inc/cache.php';
} }
function basic_error_function_because_the_other_isnt_loaded_yet($message) { 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) { function loadThemeConfig($_theme) {
global $config; global $config;
@ -478,7 +469,7 @@
} }
function checkBan($board = 0) { function checkBan($board = 0) {
global $config, $memcached; global $config;
if(!isset($_SERVER['REMOTE_ADDR'])) { if(!isset($_SERVER['REMOTE_ADDR'])) {
// Server misconfiguration // Server misconfiguration
@ -718,7 +709,7 @@
} }
function index($page, $mod=false) { function index($page, $mod=false) {
global $board, $config, $memcached, $debug; global $board, $config, $debug;
$body = ''; $body = '';
$offset = round($page*$config['threads_per_page']-$config['threads_per_page']); $offset = round($page*$config['threads_per_page']-$config['threads_per_page']);
@ -730,12 +721,8 @@
if($query->rowcount() < 1 && $page > 1) return false; if($query->rowcount() < 1 && $page > 1) return false;
while($th = $query->fetch()) { while($th = $query->fetch()) {
if(!$mod && $config['memcached']['enabled']) { if(!$mod && $config['cache']['enabled']) {
if($built = $memcached->get("thread_index_{$board['uri']}_{$th['id']}")) { if($built = cache::get("thread_index_{$board['uri']}_{$th['id']}")) {
if($config['debug']) {
$debug['memcached'][] = "thread_index_{$board['uri']}_{$th['id']}";
}
$body .= '<div id="thread_' . $th['id'] . '">' . $built . '</div>'; $body .= '<div id="thread_' . $th['id'] . '">' . $built . '</div>';
continue; continue;
} }
@ -916,11 +903,11 @@
} }
function checkMute() { function checkMute() {
global $config, $memcached; global $config, $debug;
if($config['memcached']['enabled']) { if($config['cache']['enabled']) {
// Cached mute? // 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())); error(sprintf($config['error']['youaremuted'], $mute['time'] + $mutetime - time()));
} }
} }
@ -938,9 +925,9 @@
} }
if($mute['time'] + $mutetime > time()) { if($mute['time'] + $mutetime > time()) {
if($config['memcached']['enabled']) { if($config['cache']['enabled']) {
$memcached->set("mute_${_SERVER['REMOTE_ADDR']}", $mute, $mute['time'] + $mutetime); cache::set("mute_${_SERVER['REMOTE_ADDR']}", $mute, $mute['time'] + $mutetime);
$memcached->set("mutetime_${_SERVER['REMOTE_ADDR']}", $mutetime, $mute['time'] + $mutetime); cache::set("mutetime_${_SERVER['REMOTE_ADDR']}", $mutetime, $mute['time'] + $mutetime);
} }
// Not expired yet // Not expired yet
error(sprintf($config['error']['youaremuted'], $mute['time'] + $mutetime - time())); error(sprintf($config['error']['youaremuted'], $mute['time'] + $mutetime - time()));
@ -1362,13 +1349,13 @@
} }
function buildThread($id, $return=false, $mod=false) { function buildThread($id, $return=false, $mod=false) {
global $board, $config, $memcached; global $board, $config;
$id = round($id); $id = round($id);
if($config['memcached']['enabled'] && !$mod) { if($config['cache']['enabled'] && !$mod) {
// Clear cache // Clear cache
$memcached->delete("thread_index_{$board['uri']}_{$id}"); cache::delete("thread_index_{$board['uri']}_{$id}");
$memcached->delete("thread_{$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'])); $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'])) { if(hasPermission($config['mod']['rebuild'])) {
$fieldset['Administration'] .= '<li><a href="?/rebuild">Rebuild static files</a></li>'; $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>'; $fieldset['Administration'] .= '<li><a href="?/flush">Clear cache</a></li>';
} }
if(hasPermission($config['mod']['show_config'])) { if(hasPermission($config['mod']['show_config'])) {
@ -1588,13 +1588,13 @@
); );
} elseif(preg_match('/^\/flush$/', $query)) { } elseif(preg_match('/^\/flush$/', $query)) {
if(!hasPermission($config['mod']['rebuild'])) error($config['error']['noaccess']); 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.'; $body = 'Successfully invalidated all items in the cache.';
modLog('Cleared cache'); modLog('Cleared cache');
} else { } else {
$body = $memcached->getResultMessage(); $body = 'An error occured while trying to flush cache.';
} }
echo Element('page.html', Array( echo Element('page.html', Array(

Loading…
Cancel
Save