2017-01-29 21:56:17 +00:00
|
|
|
<?php
|
|
|
|
require_once __DIR__ . "/../lib/phpfastcache/src/autoload.php";
|
2017-03-29 22:57:06 +00:00
|
|
|
require_once __DIR__ . "/../config/config.php";
|
2017-01-29 21:56:17 +00:00
|
|
|
|
|
|
|
use phpFastCache\CacheManager;
|
|
|
|
use phpFastCache\Util\Languages;
|
|
|
|
|
|
|
|
class CacheUtils {
|
|
|
|
|
2017-09-24 20:38:32 +00:00
|
|
|
private $devMode;
|
2017-01-29 21:56:17 +00:00
|
|
|
private $cacheInstance;
|
|
|
|
private $cacheItem;
|
|
|
|
private $key;
|
|
|
|
|
2017-09-24 20:38:32 +00:00
|
|
|
public function __construct($key) {
|
|
|
|
// If devMode is set, the cache will be invalidated immediately
|
|
|
|
$this->devMode = defined("DEV_MODE") || getenv("DEV_MODE") || file_exists(__DIR__ . "/dev_mode");
|
|
|
|
|
2017-01-29 21:56:17 +00:00
|
|
|
if(!is_string($key))
|
|
|
|
throw new InvalidArgumentException("Key must be a string");
|
|
|
|
|
2017-03-29 22:57:06 +00:00
|
|
|
global $config;
|
|
|
|
if(isset($config["general"]["timezone"])) {
|
|
|
|
date_default_timezone_set($config["general"]["timezone"]);
|
|
|
|
}
|
|
|
|
|
2017-01-29 21:56:17 +00:00
|
|
|
$this->cacheInstance = CacheManager::getInstance('Files', ["path" => __DIR__ . '/../cache']);
|
|
|
|
Languages::setEncoding();
|
|
|
|
$this->cacheItem = $this->cacheInstance->getItem($key);
|
|
|
|
$this->key = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheInstance() {
|
|
|
|
return $this->cacheInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheItem() {
|
|
|
|
return $this->cacheItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue() {
|
|
|
|
return $this->cacheItem->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setValue($value, $expireTime) {
|
2017-09-24 20:38:32 +00:00
|
|
|
if($this->devMode)
|
|
|
|
$expireTime = 1;
|
|
|
|
|
2017-01-29 21:56:17 +00:00
|
|
|
$this->cacheItem = $this->cacheItem->set($value)->expiresAfter($expireTime);
|
|
|
|
$this->cacheInstance->save($this->cacheItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isExpired() {
|
2017-09-24 20:38:32 +00:00
|
|
|
return $this->devMode || !$this->cacheItem->isHit();
|
2017-01-29 21:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function remove() {
|
|
|
|
$this->cacheInstance->deleteItem($this->key);
|
|
|
|
}
|
|
|
|
|
2017-04-16 14:29:25 +00:00
|
|
|
}
|