diff --git a/system/counter.php b/system/counter.php index ce699a27..7c59d40f 100644 --- a/system/counter.php +++ b/system/counter.php @@ -11,6 +11,8 @@ defined('MYAAC') or die('Direct access not allowed!'); define('COUNTER_SYNC', 10); // how often counter is synchronized with database (each x site refreshes) $views_counter = 1; // default value, must be here! + +$cache = Cache::getInstance(); if($cache->enabled()) { $value = 0; diff --git a/system/functions.php b/system/functions.php index 085fa57a..23e2d219 100644 --- a/system/functions.php +++ b/system/functions.php @@ -516,7 +516,9 @@ function template_ga_code() function template_form() { - global $cache, $template_name; + global $template_name; + + $cache = Cache::getInstance(); if($cache->enabled()) { $tmp = ''; @@ -963,8 +965,9 @@ function unsetSession($key) { } function getTopPlayers($limit = 5) { - global $cache, $config, $db; + global $config, $db; + $cache = Cache::getInstance(); $fetch_from_db = true; if($cache->enabled()) { diff --git a/system/init.php b/system/init.php index 1d06b1f2..5c68c970 100644 --- a/system/init.php +++ b/system/init.php @@ -29,7 +29,7 @@ if($config['gzip_output'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($ // cache require_once SYSTEM . 'libs/cache.php'; -$cache = Cache::getInstance($config['cache_engine'], $config['cache_prefix']); +$cache = Cache::getInstance(); // twig require_once SYSTEM . 'twig.php'; diff --git a/system/libs/cache.php b/system/libs/cache.php index 584b8a54..fb0d74f5 100644 --- a/system/libs/cache.php +++ b/system/libs/cache.php @@ -14,53 +14,72 @@ class Cache { static private $instance; - static public function getInstance($engine = '', $prefix = '') + /** + * @return Cache + */ + public static function getInstance() { - if(config('env') === 'dev') { - return new self(); - } - - if(!self::$instance) { - switch(strtolower($engine)) { - case 'apc': - require 'cache_apc.php'; - self::$instance = new Cache_APC($prefix); - break; - - case 'apcu': - require 'cache_apcu.php'; - self::$instance = new Cache_APCu($prefix); - break; - - case 'eaccelerator': - require 'cache_eaccelerator.php'; - self::$instance = new Cache_eAccelerator($prefix); - break; - - case 'xcache': - require 'cache_xcache.php'; - self::$instance = new Cache_XCache($prefix); - break; - - case 'file': - require 'cache_file.php'; - self::$instance = new Cache_File($prefix, CACHE); - break; - - case 'auto': - self::$instance = self::getInstance(self::detect(), $prefix); - break; - - default: - self::$instance = new self(); - break; - } + if (!self::$instance) { + return self::generateInstance(config('cache_engine'), config('cache_prefix')); } return self::$instance; } - static public function detect() + /** + * @param string $engine + * @param string $prefix + * @return Cache + */ + public static function generateInstance($engine = '', $prefix = '') + { + if(config('env') === 'dev') { + self::$instance = new self(); + return self::$instance; + } + + switch(strtolower($engine)) { + case 'apc': + require 'cache_apc.php'; + self::$instance = new Cache_APC($prefix); + break; + + case 'apcu': + require 'cache_apcu.php'; + self::$instance = new Cache_APCu($prefix); + break; + + case 'eaccelerator': + require 'cache_eaccelerator.php'; + self::$instance = new Cache_eAccelerator($prefix); + break; + + case 'xcache': + require 'cache_xcache.php'; + self::$instance = new Cache_XCache($prefix); + break; + + case 'file': + require 'cache_file.php'; + self::$instance = new Cache_File($prefix, CACHE); + break; + + case 'auto': + self::$instance = self::generateInstance(self::detect(), $prefix); + break; + + default: + self::$instance = new self(); + break; + } + + return self::$instance; + } + + /** + * @return string + */ + public static function detect() { if(function_exists('apc_fetch')) return 'apc'; @@ -74,6 +93,8 @@ class Cache return 'file'; } + /** + * @return bool + */ public function enabled() {return false;} } -?> diff --git a/system/libs/plugins.php b/system/libs/plugins.php index 8a6f6598..84f83455 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -74,7 +74,7 @@ class Plugins { private static $plugin = array(); public static function install($file) { - global $db, $cache; + global $db; $zip = new ZipArchive(); if($zip->open($file)) { @@ -259,6 +259,7 @@ class Plugins { } } + $cache = Cache::getInstance(); if($cache->enabled()) { $cache->delete('templates'); } @@ -283,7 +284,7 @@ class Plugins { } public static function uninstall($plugin_name) { - global $cache, $db; + global $db; $filename = BASE . 'plugins/' . $plugin_name . '.json'; if(!file_exists($filename)) { @@ -344,6 +345,7 @@ class Plugins { } if($success) { + $cache = Cache::getInstance(); if($cache->enabled()) { $cache->delete('templates'); } diff --git a/system/libs/pot/OTS_DB_MySQL.php b/system/libs/pot/OTS_DB_MySQL.php index 86ed304a..36d7867a 100644 --- a/system/libs/pot/OTS_DB_MySQL.php +++ b/system/libs/pot/OTS_DB_MySQL.php @@ -93,8 +93,9 @@ class OTS_DB_MySQL extends OTS_Base_DB $params['persistent'] = false; } - global $cache, $config; - if(isset($cache) && $cache->enabled()) { + global $config; + $cache = Cache::getInstance(); + if($cache->enabled()) { $tmp = null; $need_revalidation = true; if($cache->fetch('database_checksum', $tmp) && $tmp) { @@ -142,8 +143,10 @@ class OTS_DB_MySQL extends OTS_Base_DB public function __destruct() { - global $cache, $config; - if(isset($cache) && $cache->enabled()) { + global $config; + + $cache = Cache::getInstance(); + if($cache->enabled()) { $cache->set('database_tables', serialize($this->has_table_cache)); $cache->set('database_columns', serialize($this->has_column_cache)); $cache->set('database_checksum', serialize(sha1($config['database_host'] . '.' . $config['database_name']))); diff --git a/system/libs/pot/OTS_Groups_List.php b/system/libs/pot/OTS_Groups_List.php index e75bb75b..083615de 100644 --- a/system/libs/pot/OTS_Groups_List.php +++ b/system/libs/pot/OTS_Groups_List.php @@ -57,7 +57,7 @@ class OTS_Groups_List implements IteratorAggregate, Countable return; } - global $cache; + $cache = Cache::getInstance(); $data = array(); if($cache->enabled()) diff --git a/system/libs/visitors.php b/system/libs/visitors.php index 88bfd4f9..9657b908 100644 --- a/system/libs/visitors.php +++ b/system/libs/visitors.php @@ -14,15 +14,17 @@ class Visitors private $sessionTime; // time session will live private $data; // cached data private $cacheEnabled; + private $cache; public function __construct($sessionTime = 10) { - global $cache; - $this->cacheEnabled = ($cache && $cache->enabled()); + $this->cache = Cache::getInstance(); + + $this->cacheEnabled = $this->cache->enabled(); if($this->cacheEnabled) { $tmp = ''; - if($cache->fetch('visitors', $tmp)) + if($this->cache->fetch('visitors', $tmp)) $this->data = unserialize($tmp); else $this->data = array(); @@ -40,9 +42,8 @@ class Visitors public function __destruct() { - global $cache; if($this->cacheEnabled) - $cache->set('visitors', serialize($this->data), 120); + $this->cache->set('visitors', serialize($this->data), 120); } public function visitorExists($ip) @@ -101,10 +102,10 @@ class Visitors if($this->cacheEnabled) { foreach($this->data as $ip => &$details) $details['ip'] = $ip; - + return $this->data; } - + global $db; return $db->query('SELECT ' . $db->fieldName('ip') . ', ' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' ORDER BY ' . $db->fieldName('lastvisit') . ' DESC')->fetchAll(); } diff --git a/system/pages/admin/dashboard.php b/system/pages/admin/dashboard.php index 82631487..1e246c37 100644 --- a/system/pages/admin/dashboard.php +++ b/system/pages/admin/dashboard.php @@ -10,6 +10,7 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Dashboard'; +$cache = Cache::getInstance(); if($cache->enabled()) { if(isset($_GET['clear_cache'])) { if(clearCache()) @@ -55,7 +56,8 @@ $twig->display('admin.dashboard.html.twig', array( function clearCache() { - global $cache, $template_name; + global $template_name; + $cache = Cache::getInstance(); $tmp = ''; if($cache->fetch('status', $tmp)) diff --git a/system/pages/news.php b/system/pages/news.php index 7ac0ec9f..b461336c 100644 --- a/system/pages/news.php +++ b/system/pages/news.php @@ -97,6 +97,8 @@ if(isset($_GET['archive'])) header('X-XSS-Protection: 0'); $title = 'Latest News'; +$cache = Cache::getInstance(); + $news_cached = false; // some constants, used mainly by database (cannot by modified without schema changes) define('TITLE_LIMIT', 100); @@ -441,7 +443,9 @@ class News static public function getCached($type) { - global $cache, $template_name; + global $template_name; + + $cache = Cache::getInstance(); if($cache->enabled()) { $tmp = '';