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 preload configuration, which will cache
29 * all config values per call in a cache.
31 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
33 class PreloadConfig extends AbstractConfig
36 private $config_loaded;
39 * @param Cache $configCache The configuration cache (based on the config-files)
40 * @param Config $configRepo The configuration model
42 public function __construct(Cache $configCache, Config $configRepo)
44 parent::__construct($configCache, $configRepo);
45 $this->config_loaded = false;
53 * This loads all config values everytime load is called
55 public function load(string $cat = 'config')
57 // Don't load the whole configuration twice
58 if ($this->config_loaded) {
62 // If not connected, do nothing
63 if (!$this->configRepo->isConnected()) {
67 $config = $this->configRepo->load();
68 $this->config_loaded = true;
70 // load the whole category out of the DB into the cache
71 $this->configCache->load($config, Cache::SOURCE_DB);
77 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
80 if ($this->configRepo->isConnected()) {
81 $config = $this->configRepo->get($cat, $key);
83 $this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
88 // use the config cache for return
89 $result = $this->configCache->get($cat, $key);
91 return (isset($result)) ? $result : $default_value;
97 public function set(string $cat, string $key, $value): bool
99 if (!$this->config_loaded) {
103 // set the cache first
104 $cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
106 // If there is no connected adapter, we're finished
107 if (!$this->configRepo->isConnected()) {
111 $stored = $this->configRepo->set($cat, $key, $value);
113 return $cached && $stored;
119 public function delete(string $cat, string $key): bool
121 if ($this->config_loaded) {
125 $cacheRemoved = $this->configCache->delete($cat, $key);
127 if (!$this->configRepo->isConnected()) {
128 return $cacheRemoved;
131 $storeRemoved = $this->configRepo->delete($cat, $key);
133 return $cacheRemoved || $storeRemoved;