3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Config;
24 use Friendica\Core\BaseConfig;
28 * This class implements the Just-In-Time configuration, which will cache
29 * config values in a cache, once they are retrieved.
31 * Default Configuration type.
32 * Provides the best performance for pages loading few configuration variables.
34 class JitConfig extends BaseConfig
37 * @var array Array of already loaded db values (even if there was no value)
42 * @param Cache $configCache The configuration cache (based on the config-files)
43 * @param Model\Config\Config $configModel The configuration model
45 public function __construct(Cache $configCache, Model\Config\Config $configModel)
47 parent::__construct($configCache, $configModel);
48 $this->db_loaded = [];
57 public function load(string $cat = 'config')
59 // If not connected, do nothing
60 if (!$this->configModel->isConnected()) {
64 $config = $this->configModel->load($cat);
66 if (!empty($config[$cat])) {
67 foreach ($config[$cat] as $key => $value) {
68 $this->db_loaded[$cat][$key] = true;
72 // load the whole category out of the DB into the cache
73 $this->configCache->load($config, true);
79 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
81 // if the value isn't loaded or refresh is needed, load it to the cache
82 if ($this->configModel->isConnected() &&
83 (empty($this->db_loaded[$cat][$key]) ||
86 $dbvalue = $this->configModel->get($cat, $key);
88 if (isset($dbvalue)) {
89 $this->configCache->set($cat, $key, $dbvalue);
93 $this->db_loaded[$cat][$key] = true;
96 // use the config cache for return
97 $result = $this->configCache->get($cat, $key);
99 return (isset($result)) ? $result : $default_value;
105 public function set(string $cat, string $key, $value)
107 // set the cache first
108 $cached = $this->configCache->set($cat, $key, $value);
110 // If there is no connected adapter, we're finished
111 if (!$this->configModel->isConnected()) {
115 $stored = $this->configModel->set($cat, $key, $value);
117 $this->db_loaded[$cat][$key] = $stored;
119 return $cached && $stored;
125 public function delete(string $cat, string $key)
127 $cacheRemoved = $this->configCache->delete($cat, $key);
129 if (isset($this->db_loaded[$cat][$key])) {
130 unset($this->db_loaded[$cat][$key]);
133 if (!$this->configModel->isConnected()) {
134 return $cacheRemoved;
137 $storeRemoved = $this->configModel->delete($cat, $key);
139 return $cacheRemoved || $storeRemoved;