Add cache and options

This commit is contained in:
Matteo Gheza 2021-12-29 16:44:21 +01:00
parent 3475cc456e
commit 2b0554cd17
3 changed files with 58 additions and 2 deletions

4
backend/.gitignore vendored
View File

@ -1,2 +1,4 @@
vendor
dist-frontend
dist-frontend
tmp/*
!tmp/.gitkeep

0
backend/tmp/.gitkeep Normal file
View File

View File

@ -2,6 +2,8 @@
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key\InMemory;
use Phpfastcache\CacheManager;
use Phpfastcache\Config\ConfigurationOption;
require_once("vendor/autoload.php");
require("config.php");
@ -83,6 +85,48 @@ function logger($action, $changed=null, $editor=null, $timestamp=null)
}
}
CacheManager::setDefaultConfig(new ConfigurationOption([
'path' => realpath(dirname(__FILE__).'/tmp')
]));
$cache = CacheManager::getInstance('files');
class options
{
protected $db;
protected $cache;
public $options = [];
public $optionsCache;
public function __construct($db, $cache, $bypassCache=false){
$this->db = $db;
$this->cache = $cache;
if(!$bypassCache){
$this->optionsCache = $this->cache->getItem("options");
if (is_null($this->optionsCache->get())) {
$this->optionsCache->set($db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1"))->expiresAfter(60);
$this->cache->save($this->optionsCache);
}
$this->options = $this->optionsCache->get();
} else {
$this->options = $db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1");
}
}
public function get($name)
{
if(defined($name)) {
return constant($name);
} else {
foreach($this->options as $option){
if($name == $option["name"]) {
return empty($option["value"]) ? false : $option["value"];
}
}
throw new \Exception("Option not found: ".$name);
}
}
}
class Users
{
private $db = null;
@ -270,6 +314,16 @@ class Schedules {
}
}
$options = new Options($db, $cache);
$users = new Users($db, $auth);
$services = new Services($db);
$schedules = new Schedules($db, $users);
$schedules = new Schedules($db, $users);
function get_option($name, $default=null) {
global $options;
try {
return $options->get($name);
} catch(Exception $e) {
return $default;
}
}