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 Just-In-Time configuration, which will cache
29 * user config values in a cache, once they are retrieved.
31 * Default Configuration type.
32 * Provides the best performance for pages loading few configuration variables.
34 class JitPConfig extends BasePConfig
37 * @var array Array of already loaded db values (even if there was no value)
42 * @param Cache $configCache The configuration cache
43 * @param Model\Config\PConfig $configModel The configuration model
45 public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
47 parent::__construct($configCache, $configModel);
48 $this->db_loaded = [];
55 public function load(int $uid, string $cat = 'config')
57 // If not connected or no uid, do nothing
58 if (!$uid || !$this->configModel->isConnected()) {
62 $config = $this->configModel->load($uid, $cat);
64 if (!empty($config[$cat])) {
65 foreach ($config[$cat] as $key => $value) {
66 $this->db_loaded[$uid][$cat][$key] = true;
70 // load the whole category out of the DB into the cache
71 $this->configCache->load($uid, $config);
77 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
80 return $default_value;
83 // if the value isn't loaded or refresh is needed, load it to the cache
84 if ($this->configModel->isConnected() &&
85 (empty($this->db_loaded[$uid][$cat][$key]) ||
88 $dbvalue = $this->configModel->get($uid, $cat, $key);
90 if (isset($dbvalue)) {
91 $this->configCache->set($uid, $cat, $key, $dbvalue);
95 $this->db_loaded[$uid][$cat][$key] = true;
98 // use the config cache for return
99 $result = $this->configCache->get($uid, $cat, $key);
101 return (isset($result)) ? $result : $default_value;
107 public function set(int $uid, string $cat, string $key, $value)
113 // set the cache first
114 $cached = $this->configCache->set($uid, $cat, $key, $value);
116 // If there is no connected adapter, we're finished
117 if (!$this->configModel->isConnected()) {
121 $stored = $this->configModel->set($uid, $cat, $key, $value);
123 $this->db_loaded[$uid][$cat][$key] = $stored;
125 return $cached && $stored;
131 public function delete(int $uid, string $cat, string $key)
137 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
139 if (isset($this->db_loaded[$uid][$cat][$key])) {
140 unset($this->db_loaded[$uid][$cat][$key]);
143 if (!$this->configModel->isConnected()) {
144 return $cacheRemoved;
147 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
149 return $cacheRemoved || $storeRemoved;