From 555688e880b1bb50c13286b7d373ffd683847234 Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Fri, 7 Oct 2011 16:51:19 +1100 Subject: [PATCH] memcached, APC, xcache --- inc/cache.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ inc/config.php | 22 +++++++++---- inc/display.php | 17 +++------- inc/functions.php | 47 ++++++++++----------------- mod.php | 8 ++--- 5 files changed, 136 insertions(+), 53 deletions(-) create mode 100644 inc/cache.php diff --git a/inc/cache.php b/inc/cache.php new file mode 100644 index 00000000..8909deaf --- /dev/null +++ b/inc/cache.php @@ -0,0 +1,95 @@ +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; + } + } + diff --git a/inc/config.php b/inc/config.php index 4a99011f..2a511c04 100644 --- a/inc/config.php +++ b/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 /* * ==================== diff --git a/inc/display.php b/inc/display.php index 9b2fbe9c..fe33f4c1 100644 --- a/inc/display.php +++ b/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; diff --git a/inc/functions.php b/inc/functions.php index 5348f345..56bb011e 100644 --- a/inc/functions.php +++ b/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 .= '
' . $built . '
'; 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'])); diff --git a/mod.php b/mod.php index 792efbb0..4dee0a23 100644 --- a/mod.php +++ b/mod.php @@ -151,7 +151,7 @@ if(hasPermission($config['mod']['rebuild'])) { $fieldset['Administration'] .= '
  • Rebuild static files
  • '; } - if(hasPermission($config['mod']['rebuild']) && $config['memcached']['enabled']) { + if(hasPermission($config['mod']['rebuild']) && $config['cache']['enabled']) { $fieldset['Administration'] .= '
  • Clear cache
  • '; } 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(