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 .= '