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\PConfig\Type;
24 use Friendica\Core\PConfig\Repository;
25 use Friendica\Core\PConfig\ValueObject;
28 * This class implements the preload configuration, which will cache
29 * all user 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 PreloadPConfig extends AbstractPConfigValues
36 private $config_loaded;
39 * @param ValueObject\Cache $configCache The configuration cache
40 * @param Repository\PConfig $configRepo The configuration model
42 public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
44 parent::__construct($configCache, $configRepo);
45 $this->config_loaded = [];
51 * This loads all config values everytime load is called
54 public function load(int $uid, string $cat = 'config'): array
56 // Don't load the whole configuration twice or with invalid uid
57 if (!$uid || !empty($this->config_loaded[$uid])) {
61 // If not connected, do nothing
62 if (!$this->configModel->isConnected()) {
66 $config = $this->configModel->load($uid);
67 $this->config_loaded[$uid] = true;
69 // load the whole category out of the DB into the cache
70 $this->configCache->load($uid, $config);
78 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
81 return $default_value;
84 if (empty($this->config_loaded[$uid])) {
87 if ($this->configModel->isConnected()) {
88 $config = $this->configModel->get($uid, $cat, $key);
90 $this->configCache->set($uid, $cat, $key, $config);
95 // use the config cache for return
96 $result = $this->configCache->get($uid, $cat, $key);
98 return (isset($result)) ? $result : $default_value;
104 public function set(int $uid, string $cat, string $key, $value): bool
110 if (empty($this->config_loaded[$uid])) {
114 // set the cache first
115 $cached = $this->configCache->set($uid, $cat, $key, $value);
117 // If there is no connected adapter, we're finished
118 if (!$this->configModel->isConnected()) {
122 $stored = $this->configModel->set($uid, $cat, $key, $value);
124 return $cached && $stored;
130 public function delete(int $uid, string $cat, string $key): bool
136 if (empty($this->config_loaded[$uid])) {
140 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
142 if (!$this->configModel->isConnected()) {
143 return $cacheRemoved;
146 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
148 return $cacheRemoved || $storeRemoved;