Files
fdn2/app/private/lib/SimpleConfigHelper.php
T
2022-09-25 21:21:42 +02:00

71 lines
1.8 KiB
PHP

<?php
class SimpleConfigHelper {
private Memcached $memcached;
private GenericDao $dao;
private stdClass $config;
public function __construct() {
$this->memcached = new Memcached();
$this->memcached->addServer('memcached', 11211);
$this->dao = GlobalVariables::get("dao");
$this->reload();
}
public function hasValue(string $name) : bool {
return property_exists($this->config, $name);
}
public function getValue(string $name) {
return $this->config->$name;
}
public function setValue(string $name, $val) {
$model = $this->dao->getFirst(SimpleConfig::class);
$model->details->$name = $val;
$this->dao->save($model);
$this->reload($model);
}
private function reload($model = null) {
if (!is_null($model)) {
$this->memcached->set($this->getCacheKey(), json_encode($model->details, JSON_FORCE_OBJECT));
}
else if ( !$this->isCached() ) {
$model = $this->dao->getFirst(SimpleConfig::class);
if (is_null($model)) {
$model = new SimpleConfig();
$this->dao->save($model);
}
$this->memcached->set($this->getCacheKey(), json_encode($model->details, JSON_FORCE_OBJECT));
}
$rval = $this->memcached->get(
$this->getCacheKey()
);
$this->config = json_decode($rval, false);
}
private function getCacheKey() {
return 'glob.fdn2.simpleConfig';
}
private function isCached(): bool {
return $this->memcached->get( $this->getCacheKey() ) ? true : false;
}
private function flush() {
$this->memcached->set( $this->getCacheKey(), false );
}
}
?>