* 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)
$views_counter = 1; // default value, must be here!
$cache = Cache::getInstance();
if($cache->enabled())
{
$value = 0;

View File

@ -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())
{

View File

@ -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';

View File

@ -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;}
}
?>

View File

@ -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');
}

View File

@ -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'])));

View File

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

View File

@ -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();
}

View File

@ -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))

View File

@ -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 = '';