]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/Type/JitPConfig.php
Merge pull request #12078 from MrPetovan/task/4090-move-mod-wallmessage
[friendica.git] / src / Core / PConfig / Type / JitPConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core\PConfig\Type;
23
24 use Friendica\Core\PConfig\Repository;
25 use Friendica\Core\PConfig\ValueObject;
26
27 /**
28  * This class implements the Just-In-Time configuration, which will cache
29  * user config values in a cache, once they are retrieved.
30  *
31  * Default Configuration type.
32  * Provides the best performance for pages loading few configuration variables.
33  */
34 class JitPConfig extends AbstractPConfigValues
35 {
36         /**
37          * @var array Array of already loaded db values (even if there was no value)
38          */
39         private $db_loaded;
40
41         /**
42          * @param ValueObject\Cache  $configCache The configuration cache
43          * @param Repository\PConfig $configRepo  The configuration model
44          */
45         public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
46         {
47                 parent::__construct($configCache, $configRepo);
48                 $this->db_loaded = [];
49         }
50
51         /**
52          * {@inheritDoc}
53          *
54          */
55         public function load(int $uid, string $cat = 'config'): array
56         {
57                 // If not connected or no uid, do nothing
58                 if (!$uid || !$this->configModel->isConnected()) {
59                         return [];
60                 }
61
62                 $config = $this->configModel->load($uid, $cat);
63
64                 if (!empty($config[$cat])) {
65                         foreach ($config[$cat] as $key => $value) {
66                                 $this->db_loaded[$uid][$cat][$key] = true;
67                         }
68                 }
69
70                 // load the whole category out of the DB into the cache
71                 $this->configCache->load($uid, $config);
72
73                 return $config;
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
80         {
81                 if (!$uid) {
82                         return $default_value;
83                 }
84
85                 // if the value isn't loaded or refresh is needed, load it to the cache
86                 if ($this->configModel->isConnected() &&
87                         (empty($this->db_loaded[$uid][$cat][$key]) || $refresh)) {
88                         $dbValue = $this->configModel->get($uid, $cat, $key);
89
90                         if (isset($dbValue)) {
91                                 $this->configCache->set($uid, $cat, $key, $dbValue);
92                                 unset($dbValue);
93                         }
94
95                         $this->db_loaded[$uid][$cat][$key] = true;
96                 }
97
98                 // use the config cache for return
99                 $result = $this->configCache->get($uid, $cat, $key);
100
101                 return (isset($result)) ? $result : $default_value;
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         public function set(int $uid, string $cat, string $key, $value): bool
108         {
109                 if (!$uid) {
110                         return false;
111                 }
112
113                 // set the cache first
114                 $cached = $this->configCache->set($uid, $cat, $key, $value);
115
116                 // If there is no connected adapter, we're finished
117                 if (!$this->configModel->isConnected()) {
118                         return $cached;
119                 }
120
121                 $stored = $this->configModel->set($uid, $cat, $key, $value);
122
123                 $this->db_loaded[$uid][$cat][$key] = $stored;
124
125                 return $cached && $stored;
126         }
127
128         /**
129          * {@inheritDoc}
130          */
131         public function delete(int $uid, string $cat, string $key): bool
132         {
133                 if (!$uid) {
134                         return false;
135                 }
136
137                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
138
139                 if (isset($this->db_loaded[$uid][$cat][$key])) {
140                         unset($this->db_loaded[$uid][$cat][$key]);
141                 }
142
143                 if (!$this->configModel->isConnected()) {
144                         return $cacheRemoved;
145                 }
146
147                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
148
149                 return $cacheRemoved || $storeRemoved;
150         }
151 }