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\PConfig;
24 use Friendica\Core\BasePConfig;
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 BasePConfig
36 private $config_loaded;
39 * @param Cache $configCache The configuration cache
40 * @param Model\Config\PConfig $configModel The configuration model
42 public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
44 parent::__construct($configCache, $configModel);
45 $this->config_loaded = [];
51 * This loads all config values everytime load is called
54 public function load(int $uid, string $cat = 'config')
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);
76 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
79 return $default_value;
82 if (empty($this->config_loaded[$uid])) {
85 if ($this->configModel->isConnected()) {
86 $config = $this->configModel->get($uid, $cat, $key);
88 $this->configCache->set($uid, $cat, $key, $config);
93 // use the config cache for return
94 $result = $this->configCache->get($uid, $cat, $key);
96 return (isset($result)) ? $result : $default_value;
102 public function set(int $uid, string $cat, string $key, $value)
108 if (empty($this->config_loaded[$uid])) {
112 // set the cache first
113 $cached = $this->configCache->set($uid, $cat, $key, $value);
115 // If there is no connected adapter, we're finished
116 if (!$this->configModel->isConnected()) {
120 $stored = $this->configModel->set($uid, $cat, $key, $value);
122 return $cached && $stored;
128 public function delete(int $uid, string $cat, string $key)
134 if (empty($this->config_loaded[$uid])) {
138 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
140 if (!$this->configModel->isConnected()) {
141 return $cacheRemoved;
144 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
146 return $cacheRemoved || $storeRemoved;