* stop using global $cache variable, use Singleton pattern instead

This commit is contained in:
slawkens 2018-06-03 17:09:21 +02:00
parent 45a387000d
commit fb1b9fea09
10 changed files with 99 additions and 61 deletions

View File

@ -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) define('COUNTER_SYNC', 10); // how often counter is synchronized with database (each x site refreshes)
$views_counter = 1; // default value, must be here! $views_counter = 1; // default value, must be here!
$cache = Cache::getInstance();
if($cache->enabled()) if($cache->enabled())
{ {
$value = 0; $value = 0;

View File

@ -516,7 +516,9 @@ function template_ga_code()
function template_form() function template_form()
{ {
global $cache, $template_name; global $template_name;
$cache = Cache::getInstance();
if($cache->enabled()) if($cache->enabled())
{ {
$tmp = ''; $tmp = '';
@ -963,8 +965,9 @@ function unsetSession($key) {
} }
function getTopPlayers($limit = 5) { function getTopPlayers($limit = 5) {
global $cache, $config, $db; global $config, $db;
$cache = Cache::getInstance();
$fetch_from_db = true; $fetch_from_db = true;
if($cache->enabled()) if($cache->enabled())
{ {

View File

@ -29,7 +29,7 @@ if($config['gzip_output'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($
// cache // cache
require_once SYSTEM . 'libs/cache.php'; require_once SYSTEM . 'libs/cache.php';
$cache = Cache::getInstance($config['cache_engine'], $config['cache_prefix']); $cache = Cache::getInstance();
// twig // twig
require_once SYSTEM . 'twig.php'; require_once SYSTEM . 'twig.php';

View File

@ -14,53 +14,72 @@ class Cache
{ {
static private $instance; static private $instance;
static public function getInstance($engine = '', $prefix = '') /**
* @return Cache
*/
public static function getInstance()
{ {
if(config('env') === 'dev') { if (!self::$instance) {
return new self(); return self::generateInstance(config('cache_engine'), config('cache_prefix'));
}
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;
}
} }
return self::$instance; 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')) if(function_exists('apc_fetch'))
return 'apc'; return 'apc';
@ -74,6 +93,8 @@ class Cache
return 'file'; return 'file';
} }
/**
* @return bool
*/
public function enabled() {return false;} public function enabled() {return false;}
} }
?>

View File

@ -74,7 +74,7 @@ class Plugins {
private static $plugin = array(); private static $plugin = array();
public static function install($file) { public static function install($file) {
global $db, $cache; global $db;
$zip = new ZipArchive(); $zip = new ZipArchive();
if($zip->open($file)) { if($zip->open($file)) {
@ -259,6 +259,7 @@ class Plugins {
} }
} }
$cache = Cache::getInstance();
if($cache->enabled()) { if($cache->enabled()) {
$cache->delete('templates'); $cache->delete('templates');
} }
@ -283,7 +284,7 @@ class Plugins {
} }
public static function uninstall($plugin_name) { public static function uninstall($plugin_name) {
global $cache, $db; global $db;
$filename = BASE . 'plugins/' . $plugin_name . '.json'; $filename = BASE . 'plugins/' . $plugin_name . '.json';
if(!file_exists($filename)) { if(!file_exists($filename)) {
@ -344,6 +345,7 @@ class Plugins {
} }
if($success) { if($success) {
$cache = Cache::getInstance();
if($cache->enabled()) { if($cache->enabled()) {
$cache->delete('templates'); $cache->delete('templates');
} }

View File

@ -93,8 +93,9 @@ class OTS_DB_MySQL extends OTS_Base_DB
$params['persistent'] = false; $params['persistent'] = false;
} }
global $cache, $config; global $config;
if(isset($cache) && $cache->enabled()) { $cache = Cache::getInstance();
if($cache->enabled()) {
$tmp = null; $tmp = null;
$need_revalidation = true; $need_revalidation = true;
if($cache->fetch('database_checksum', $tmp) && $tmp) { if($cache->fetch('database_checksum', $tmp) && $tmp) {
@ -142,8 +143,10 @@ class OTS_DB_MySQL extends OTS_Base_DB
public function __destruct() public function __destruct()
{ {
global $cache, $config; global $config;
if(isset($cache) && $cache->enabled()) {
$cache = Cache::getInstance();
if($cache->enabled()) {
$cache->set('database_tables', serialize($this->has_table_cache)); $cache->set('database_tables', serialize($this->has_table_cache));
$cache->set('database_columns', serialize($this->has_column_cache)); $cache->set('database_columns', serialize($this->has_column_cache));
$cache->set('database_checksum', serialize(sha1($config['database_host'] . '.' . $config['database_name']))); $cache->set('database_checksum', serialize(sha1($config['database_host'] . '.' . $config['database_name'])));

View File

@ -57,7 +57,7 @@ class OTS_Groups_List implements IteratorAggregate, Countable
return; return;
} }
global $cache; $cache = Cache::getInstance();
$data = array(); $data = array();
if($cache->enabled()) if($cache->enabled())

View File

@ -14,15 +14,17 @@ class Visitors
private $sessionTime; // time session will live private $sessionTime; // time session will live
private $data; // cached data private $data; // cached data
private $cacheEnabled; private $cacheEnabled;
private $cache;
public function __construct($sessionTime = 10) public function __construct($sessionTime = 10)
{ {
global $cache; $this->cache = Cache::getInstance();
$this->cacheEnabled = ($cache && $cache->enabled());
$this->cacheEnabled = $this->cache->enabled();
if($this->cacheEnabled) if($this->cacheEnabled)
{ {
$tmp = ''; $tmp = '';
if($cache->fetch('visitors', $tmp)) if($this->cache->fetch('visitors', $tmp))
$this->data = unserialize($tmp); $this->data = unserialize($tmp);
else else
$this->data = array(); $this->data = array();
@ -40,9 +42,8 @@ class Visitors
public function __destruct() public function __destruct()
{ {
global $cache;
if($this->cacheEnabled) if($this->cacheEnabled)
$cache->set('visitors', serialize($this->data), 120); $this->cache->set('visitors', serialize($this->data), 120);
} }
public function visitorExists($ip) public function visitorExists($ip)

View File

@ -10,6 +10,7 @@
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
$title = 'Dashboard'; $title = 'Dashboard';
$cache = Cache::getInstance();
if($cache->enabled()) { if($cache->enabled()) {
if(isset($_GET['clear_cache'])) { if(isset($_GET['clear_cache'])) {
if(clearCache()) if(clearCache())
@ -55,7 +56,8 @@ $twig->display('admin.dashboard.html.twig', array(
function clearCache() function clearCache()
{ {
global $cache, $template_name; global $template_name;
$cache = Cache::getInstance();
$tmp = ''; $tmp = '';
if($cache->fetch('status', $tmp)) if($cache->fetch('status', $tmp))

View File

@ -97,6 +97,8 @@ if(isset($_GET['archive']))
header('X-XSS-Protection: 0'); header('X-XSS-Protection: 0');
$title = 'Latest News'; $title = 'Latest News';
$cache = Cache::getInstance();
$news_cached = false; $news_cached = false;
// some constants, used mainly by database (cannot by modified without schema changes) // some constants, used mainly by database (cannot by modified without schema changes)
define('TITLE_LIMIT', 100); define('TITLE_LIMIT', 100);
@ -441,7 +443,9 @@ class News
static public function getCached($type) static public function getCached($type)
{ {
global $cache, $template_name; global $template_name;
$cache = Cache::getInstance();
if($cache->enabled()) if($cache->enabled())
{ {
$tmp = ''; $tmp = '';