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 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 BaseConfig
36 private $config_loaded;
39 * @param Cache $configCache The configuration cache (based on the config-files)
40 * @param Model\Config\Config $configModel The configuration model
42 public function __construct(Cache $configCache, Model\Config\Config $configModel)
44 parent::__construct($configCache, $configModel);
45 $this->config_loaded = false;
53 * This loads all config values everytime load is called
56 public function load(string $cat = 'config')
58 // Don't load the whole configuration twice
59 if ($this->config_loaded) {
63 // If not connected, do nothing
64 if (!$this->configModel->isConnected()) {
68 $config = $this->configModel->load();
69 $this->config_loaded = true;
71 // load the whole category out of the DB into the cache
72 $this->configCache->load($config, true);
78 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
81 if ($this->configModel->isConnected()) {
82 $config = $this->configModel->get($cat, $key);
84 $this->configCache->set($cat, $key, $config);
89 // use the config cache for return
90 $result = $this->configCache->get($cat, $key);
92 return (isset($result)) ? $result : $default_value;
98 public function set(string $cat, string $key, $value)
100 if (!$this->config_loaded) {
104 // set the cache first
105 $cached = $this->configCache->set($cat, $key, $value);
107 // If there is no connected adapter, we're finished
108 if (!$this->configModel->isConnected()) {
112 $stored = $this->configModel->set($cat, $key, $value);
114 return $cached && $stored;
120 public function delete(string $cat, string $key)
122 if ($this->config_loaded) {
126 $cacheRemoved = $this->configCache->delete($cat, $key);
128 if (!$this->configModel->isConnected()) {
129 return $cacheRemoved;
132 $storeRemoved = $this->configModel->delete($cat, $key);
134 return $cacheRemoved || $storeRemoved;