* 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

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