3 namespace Friendica\Core\Config;
8 * This class implements the Just-In-Time configuration, which will cache
9 * config values in a cache, once they are retrieved.
11 * Default Configuration type.
12 * Provides the best performance for pages loading few configuration variables.
14 class JitConfiguration extends Configuration
17 * @var array Array of already loaded db values (even if there was no value)
22 * @param Cache\ConfigCache $configCache The configuration cache (based on the config-files)
23 * @param Model\Config\Config $configModel The configuration model
25 public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
27 parent::__construct($configCache, $configModel);
28 $this->db_loaded = [];
37 public function load(string $cat = 'config')
39 // If not connected, do nothing
40 if (!$this->configModel->isConnected()) {
44 $config = $this->configModel->load($cat);
46 if (!empty($config[$cat])) {
47 foreach ($config[$cat] as $key => $value) {
48 $this->db_loaded[$cat][$key] = true;
52 // load the whole category out of the DB into the cache
53 $this->configCache->load($config, true);
59 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
61 // if the value isn't loaded or refresh is needed, load it to the cache
62 if ($this->configModel->isConnected() &&
63 (empty($this->db_loaded[$cat][$key]) ||
66 $dbvalue = $this->configModel->get($cat, $key);
68 if (isset($dbvalue)) {
69 $this->configCache->set($cat, $key, $dbvalue);
73 $this->db_loaded[$cat][$key] = true;
76 // use the config cache for return
77 $result = $this->configCache->get($cat, $key);
79 return (isset($result)) ? $result : $default_value;
85 public function set(string $cat, string $key, $value)
87 // set the cache first
88 $cached = $this->configCache->set($cat, $key, $value);
90 // If there is no connected adapter, we're finished
91 if (!$this->configModel->isConnected()) {
95 $stored = $this->configModel->set($cat, $key, $value);
97 $this->db_loaded[$cat][$key] = $stored;
99 return $cached && $stored;
105 public function delete(string $cat, string $key)
107 $cacheRemoved = $this->configCache->delete($cat, $key);
109 if (isset($this->db_loaded[$cat][$key])) {
110 unset($this->db_loaded[$cat][$key]);
113 if (!$this->configModel->isConnected()) {
114 return $cacheRemoved;
117 $storeRemoved = $this->configModel->delete($cat, $key);
119 return $cacheRemoved || $storeRemoved;