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
20 * @param Cache\ConfigCache $configCache The configuration cache (based on the config-files)
21 * @param Model\Config\Config $configModel The configuration model
23 public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
25 parent::__construct($configCache, $configModel);
28 // take the values of the given cache instead of loading them from the model again
29 $preSet = $configCache->getAll();
30 if (!empty($preSet)) {
31 foreach ($preSet as $cat => $data) {
32 foreach ($data as $key => $value) {
33 $this->in_db[$cat][$key] = true;
45 public function load(string $cat = 'config')
47 // If not connected, do nothing
48 if (!$this->configModel->isConnected()) {
52 $config = $this->configModel->load($cat);
54 if (!empty($config[$cat])) {
55 foreach ($config[$cat] as $key => $value) {
56 $this->in_db[$cat][$key] = true;
60 // load the whole category out of the DB into the cache
61 $this->configCache->load($config, true);
67 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
69 // if the value isn't loaded or refresh is needed, load it to the cache
70 if ($this->configModel->isConnected() &&
71 (empty($this->in_db[$cat][$key]) ||
74 $dbvalue = $this->configModel->get($cat, $key);
76 if (isset($dbvalue)) {
77 $this->configCache->set($cat, $key, $dbvalue);
79 $this->in_db[$cat][$key] = true;
83 // use the config cache for return
84 $result = $this->configCache->get($cat, $key);
86 return (isset($result)) ? $result : $default_value;
92 public function set(string $cat, string $key, $value)
94 // set the cache first
95 $cached = $this->configCache->set($cat, $key, $value);
97 // If there is no connected adapter, we're finished
98 if (!$this->configModel->isConnected()) {
102 $stored = $this->configModel->set($cat, $key, $value);
104 $this->in_db[$cat][$key] = $stored;
106 return $cached && $stored;
112 public function delete(string $cat, string $key)
114 $cacheRemoved = $this->configCache->delete($cat, $key);
116 if (isset($this->in_db[$cat][$key])) {
117 unset($this->in_db[$cat][$key]);
120 if (!$this->configModel->isConnected()) {
121 return $cacheRemoved;
124 $storeRemoved = $this->configModel->delete($cat, $key);
126 return $cacheRemoved || $storeRemoved;