]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/JitPConfig.php
f7dde9b2a5688c6bf5af1c1b0bf9224d940eb834
[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
74         /**
75          * {@inheritDoc}
76          */
77         public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
78         {
79                 if (!$uid) {
80                         return $default_value;
81                 }
82
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]) ||
86                      $refresh)) {
87
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)
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)
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 }