2019-07-03 00:42:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Config;
|
|
|
|
|
|
|
|
use Friendica\Model;
|
|
|
|
|
|
|
|
/**
|
2019-07-14 22:41:09 +02:00
|
|
|
* This class implements the Just-In-Time configuration, which will cache
|
|
|
|
* config values in a cache, once they are retrieved.
|
|
|
|
*
|
|
|
|
* Default Configuration type.
|
|
|
|
* Provides the best performance for pages loading few configuration variables.
|
2019-07-03 00:42:47 +02:00
|
|
|
*/
|
|
|
|
class JitConfiguration extends Configuration
|
|
|
|
{
|
2019-07-15 08:58:37 +02:00
|
|
|
/**
|
|
|
|
* @var array Array of already loaded db values (even if there was no value)
|
|
|
|
*/
|
|
|
|
private $db_loaded;
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Cache\ConfigCache $configCache The configuration cache (based on the config-files)
|
|
|
|
* @param Model\Config\Config $configModel The configuration model
|
|
|
|
*/
|
|
|
|
public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
|
|
|
|
{
|
|
|
|
parent::__construct($configCache, $configModel);
|
2019-07-15 08:58:37 +02:00
|
|
|
$this->db_loaded = [];
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function load(string $cat = 'config')
|
|
|
|
{
|
|
|
|
// If not connected, do nothing
|
|
|
|
if (!$this->configModel->isConnected()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = $this->configModel->load($cat);
|
|
|
|
|
2019-07-04 21:39:49 +02:00
|
|
|
if (!empty($config[$cat])) {
|
|
|
|
foreach ($config[$cat] as $key => $value) {
|
2019-07-15 08:58:37 +02:00
|
|
|
$this->db_loaded[$cat][$key] = true;
|
2019-07-04 21:39:49 +02:00
|
|
|
}
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// load the whole category out of the DB into the cache
|
|
|
|
$this->configCache->load($config, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
|
|
|
|
{
|
|
|
|
// if the value isn't loaded or refresh is needed, load it to the cache
|
|
|
|
if ($this->configModel->isConnected() &&
|
2019-07-15 08:58:37 +02:00
|
|
|
(empty($this->db_loaded[$cat][$key]) ||
|
2019-07-03 00:42:47 +02:00
|
|
|
$refresh)) {
|
|
|
|
|
|
|
|
$dbvalue = $this->configModel->get($cat, $key);
|
|
|
|
|
|
|
|
if (isset($dbvalue)) {
|
|
|
|
$this->configCache->set($cat, $key, $dbvalue);
|
|
|
|
unset($dbvalue);
|
|
|
|
}
|
2019-07-15 08:58:37 +02:00
|
|
|
|
|
|
|
$this->db_loaded[$cat][$key] = true;
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// use the config cache for return
|
|
|
|
$result = $this->configCache->get($cat, $key);
|
|
|
|
|
|
|
|
return (isset($result)) ? $result : $default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function set(string $cat, string $key, $value)
|
|
|
|
{
|
|
|
|
// set the cache first
|
|
|
|
$cached = $this->configCache->set($cat, $key, $value);
|
|
|
|
|
|
|
|
// If there is no connected adapter, we're finished
|
|
|
|
if (!$this->configModel->isConnected()) {
|
|
|
|
return $cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stored = $this->configModel->set($cat, $key, $value);
|
|
|
|
|
2019-07-15 08:58:37 +02:00
|
|
|
$this->db_loaded[$cat][$key] = $stored;
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
return $cached && $stored;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-14 22:41:09 +02:00
|
|
|
* {@inheritDoc}
|
2019-07-03 00:42:47 +02:00
|
|
|
*/
|
|
|
|
public function delete(string $cat, string $key)
|
|
|
|
{
|
|
|
|
$cacheRemoved = $this->configCache->delete($cat, $key);
|
|
|
|
|
2019-07-15 08:58:37 +02:00
|
|
|
if (isset($this->db_loaded[$cat][$key])) {
|
|
|
|
unset($this->db_loaded[$cat][$key]);
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->configModel->isConnected()) {
|
|
|
|
return $cacheRemoved;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storeRemoved = $this->configModel->delete($cat, $key);
|
|
|
|
|
|
|
|
return $cacheRemoved || $storeRemoved;
|
|
|
|
}
|
|
|
|
}
|