3 namespace Friendica\Core\Config;
5 use Friendica\Core\BaseConfig;
9 * This class implements the Just-In-Time configuration, which will cache
10 * config values in a cache, once they are retrieved.
12 * Default Configuration type.
13 * Provides the best performance for pages loading few configuration variables.
15 class JitConfig extends BaseConfig
18 * @var array Array of already loaded db values (even if there was no value)
23 * @param Cache $configCache The configuration cache (based on the config-files)
24 * @param Model\Config\Config $configModel The configuration model
26 public function __construct(Cache $configCache, Model\Config\Config $configModel)
28 parent::__construct($configCache, $configModel);
29 $this->db_loaded = [];
38 public function load(string $cat = 'config')
40 // If not connected, do nothing
41 if (!$this->configModel->isConnected()) {
45 $config = $this->configModel->load($cat);
47 if (!empty($config[$cat])) {
48 foreach ($config[$cat] as $key => $value) {
49 $this->db_loaded[$cat][$key] = true;
53 // load the whole category out of the DB into the cache
54 $this->configCache->load($config, true);
60 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
62 // if the value isn't loaded or refresh is needed, load it to the cache
63 if ($this->configModel->isConnected() &&
64 (empty($this->db_loaded[$cat][$key]) ||
67 $dbvalue = $this->configModel->get($cat, $key);
69 if (isset($dbvalue)) {
70 $this->configCache->set($cat, $key, $dbvalue);
74 $this->db_loaded[$cat][$key] = true;
77 // use the config cache for return
78 $result = $this->configCache->get($cat, $key);
80 return (isset($result)) ? $result : $default_value;
86 public function set(string $cat, string $key, $value)
88 // set the cache first
89 $cached = $this->configCache->set($cat, $key, $value);
91 // If there is no connected adapter, we're finished
92 if (!$this->configModel->isConnected()) {
96 $stored = $this->configModel->set($cat, $key, $value);
98 $this->db_loaded[$cat][$key] = $stored;
100 return $cached && $stored;
106 public function delete(string $cat, string $key)
108 $cacheRemoved = $this->configCache->delete($cat, $key);
110 if (isset($this->db_loaded[$cat][$key])) {
111 unset($this->db_loaded[$cat][$key]);
114 if (!$this->configModel->isConnected()) {
115 return $cacheRemoved;
118 $storeRemoved = $this->configModel->delete($cat, $key);
120 return $cacheRemoved || $storeRemoved;