]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JitConfig.php
Merge pull request #9039 from MrPetovan/task/frio-accent-scheme
[friendica.git] / src / Core / Config / JitConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Config;
23
24 use Friendica\Core\BaseConfig;
25 use Friendica\Model;
26
27 /**
28  * This class implements the Just-In-Time configuration, which will cache
29  * 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 JitConfig extends BaseConfig
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 (based on the config-files)
43          * @param Model\Config\Config $configModel The configuration model
44          */
45         public function __construct(Cache $configCache, Model\Config\Config $configModel)
46         {
47                 parent::__construct($configCache, $configModel);
48                 $this->db_loaded = [];
49
50                 $this->load();
51         }
52
53         /**
54          * {@inheritDoc}
55          *
56          */
57         public function load(string $cat = 'config')
58         {
59                 // If not connected, do nothing
60                 if (!$this->configModel->isConnected()) {
61                         return;
62                 }
63
64                 $config = $this->configModel->load($cat);
65
66                 if (!empty($config[$cat])) {
67                         foreach ($config[$cat] as $key => $value) {
68                                 $this->db_loaded[$cat][$key] = true;
69                         }
70                 }
71
72                 // load the whole category out of the DB into the cache
73                 $this->configCache->load($config, true);
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
80         {
81                 // if the value isn't loaded or refresh is needed, load it to the cache
82                 if ($this->configModel->isConnected() &&
83                     (empty($this->db_loaded[$cat][$key]) ||
84                      $refresh)) {
85
86                         $dbvalue = $this->configModel->get($cat, $key);
87
88                         if (isset($dbvalue)) {
89                                 $this->configCache->set($cat, $key, $dbvalue);
90                                 unset($dbvalue);
91                         }
92
93                         $this->db_loaded[$cat][$key] = true;
94                 }
95
96                 // use the config cache for return
97                 $result = $this->configCache->get($cat, $key);
98
99                 return (isset($result)) ? $result : $default_value;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public function set(string $cat, string $key, $value)
106         {
107                 // set the cache first
108                 $cached = $this->configCache->set($cat, $key, $value);
109
110                 // If there is no connected adapter, we're finished
111                 if (!$this->configModel->isConnected()) {
112                         return $cached;
113                 }
114
115                 $stored = $this->configModel->set($cat, $key, $value);
116
117                 $this->db_loaded[$cat][$key] = $stored;
118
119                 return $cached && $stored;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         public function delete(string $cat, string $key)
126         {
127                 $cacheRemoved = $this->configCache->delete($cat, $key);
128
129                 if (isset($this->db_loaded[$cat][$key])) {
130                         unset($this->db_loaded[$cat][$key]);
131                 }
132
133                 if (!$this->configModel->isConnected()) {
134                         return $cacheRemoved;
135                 }
136
137                 $storeRemoved = $this->configModel->delete($cat, $key);
138
139                 return $cacheRemoved || $storeRemoved;
140         }
141 }