]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/JitPConfig.php
Add "user config" console command to administer user-specific configuration
[friendica.git] / src / Core / PConfig / JitPConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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;
23
24 use Friendica\Core\BasePConfig;
25 use Friendica\Model;
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 BasePConfig
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 Cache                $configCache The configuration cache
43          * @param Model\Config\PConfig $configModel The configuration model
44          */
45         public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
46         {
47                 parent::__construct($configCache, $configModel);
48                 $this->db_loaded = [];
49         }
50
51         /**
52          * {@inheritDoc}
53          *
54          */
55         public function load(int $uid, string $cat = 'config')
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]) ||
88                      $refresh)) {
89
90                         $dbvalue = $this->configModel->get($uid, $cat, $key);
91
92                         if (isset($dbvalue)) {
93                                 $this->configCache->set($uid, $cat, $key, $dbvalue);
94                                 unset($dbvalue);
95                         }
96
97                         $this->db_loaded[$uid][$cat][$key] = true;
98                 }
99
100                 // use the config cache for return
101                 $result = $this->configCache->get($uid, $cat, $key);
102
103                 return (isset($result)) ? $result : $default_value;
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         public function set(int $uid, string $cat, string $key, $value)
110         {
111                 if (!$uid) {
112                         return false;
113                 }
114
115                 // set the cache first
116                 $cached = $this->configCache->set($uid, $cat, $key, $value);
117
118                 // If there is no connected adapter, we're finished
119                 if (!$this->configModel->isConnected()) {
120                         return $cached;
121                 }
122
123                 $stored = $this->configModel->set($uid, $cat, $key, $value);
124
125                 $this->db_loaded[$uid][$cat][$key] = $stored;
126
127                 return $cached && $stored;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         public function delete(int $uid, string $cat, string $key)
134         {
135                 if (!$uid) {
136                         return false;
137                 }
138
139                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
140
141                 if (isset($this->db_loaded[$uid][$cat][$key])) {
142                         unset($this->db_loaded[$uid][$cat][$key]);
143                 }
144
145                 if (!$this->configModel->isConnected()) {
146                         return $cacheRemoved;
147                 }
148
149                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
150
151                 return $cacheRemoved || $storeRemoved;
152         }
153 }