3 * @copyright Copyright (C) 2010-2023, 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
35 const NAME = 'preload';
38 private $config_loaded;
41 * @param ValueObject\Cache $configCache The configuration cache
42 * @param Repository\PConfig $configRepo The configuration model
44 public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
46 parent::__construct($configCache, $configRepo);
47 $this->config_loaded = [];
53 * This loads all config values everytime load is called
56 public function load(int $uid, string $cat = 'config'): array
58 // Don't load the whole configuration twice or with invalid uid
59 if (!$uid || !empty($this->config_loaded[$uid])) {
63 // If not connected, do nothing
64 if (!$this->configModel->isConnected()) {
68 $config = $this->configModel->load($uid);
69 $this->config_loaded[$uid] = true;
71 // load the whole category out of the DB into the cache
72 $this->configCache->load($uid, $config);
80 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
83 return $default_value;
86 if (empty($this->config_loaded[$uid])) {
89 if ($this->configModel->isConnected()) {
90 $config = $this->configModel->get($uid, $cat, $key);
92 $this->configCache->set($uid, $cat, $key, $config);
97 // use the config cache for return
98 $result = $this->configCache->get($uid, $cat, $key);
100 return (isset($result)) ? $result : $default_value;
106 public function set(int $uid, string $cat, string $key, $value): bool
112 if (empty($this->config_loaded[$uid])) {
116 // set the cache first
117 $cached = $this->configCache->set($uid, $cat, $key, $value);
119 // If there is no connected adapter, we're finished
120 if (!$this->configModel->isConnected()) {
124 $stored = $this->configModel->set($uid, $cat, $key, $value);
126 return $cached && $stored;
132 public function delete(int $uid, string $cat, string $key): bool
138 if (empty($this->config_loaded[$uid])) {
142 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
144 if (!$this->configModel->isConnected()) {
145 return $cacheRemoved;
148 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
150 return $cacheRemoved || $storeRemoved;