3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Type;
24 use Friendica\Core\Config\ValueObject\Cache;
25 use Friendica\Core\Config\Repository\Config;
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 AbstractConfig
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 Config $configRepo The configuration model
45 public function __construct(Cache $configCache, Config $configRepo)
47 parent::__construct($configCache, $configRepo);
48 $this->db_loaded = [];
56 public function load(string $cat = 'config')
58 // If not connected, do nothing
59 if (!$this->configRepo->isConnected()) {
63 $config = $this->configRepo->load($cat);
65 if (!empty($config[$cat])) {
66 foreach ($config[$cat] as $key => $value) {
67 $this->db_loaded[$cat][$key] = true;
71 // load the whole category out of the DB into the cache
72 $this->configCache->load($config, Cache::SOURCE_DB);
78 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
80 // if the value isn't loaded or refresh is needed, load it to the cache
81 if ($this->configRepo->isConnected() &&
82 (empty($this->db_loaded[$cat][$key]) ||
84 $dbValue = $this->configRepo->get($cat, $key);
86 if (isset($dbValue)) {
87 $this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DB);
91 $this->db_loaded[$cat][$key] = true;
94 // use the config cache for return
95 $result = $this->configCache->get($cat, $key);
97 return (isset($result)) ? $result : $default_value;
103 public function set(string $cat, string $key, $value): bool
105 // set the cache first
106 $cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
108 // If there is no connected adapter, we're finished
109 if (!$this->configRepo->isConnected()) {
113 $stored = $this->configRepo->set($cat, $key, $value);
115 $this->db_loaded[$cat][$key] = $stored;
117 return $cached && $stored;
123 public function delete(string $cat, string $key): bool
125 $cacheRemoved = $this->configCache->delete($cat, $key);
127 if (isset($this->db_loaded[$cat][$key])) {
128 unset($this->db_loaded[$cat][$key]);
131 if (!$this->configRepo->isConnected()) {
132 return $cacheRemoved;
135 $storeRemoved = $this->configRepo->delete($cat, $key);
137 return $cacheRemoved || $storeRemoved;