]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Type/PreloadConfig.php
Refactoring Core class structures ...
[friendica.git] / src / Core / Config / Type / PreloadConfig.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\Config\Type;
23
24 use Friendica\Core\Config\ValueObject\Cache;
25 use Friendica\Core\Config\Repository\Config;
26
27 /**
28  * This class implements the preload configuration, which will cache
29  * all config values per call in a cache.
30  *
31  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
32  */
33 class PreloadConfig extends AbstractConfig
34 {
35         /** @var bool */
36         private $config_loaded;
37
38         /**
39          * @param Cache  $configCache The configuration cache (based on the config-files)
40          * @param Config $configRepo  The configuration model
41          */
42         public function __construct(Cache $configCache, Config $configRepo)
43         {
44                 parent::__construct($configCache, $configRepo);
45                 $this->config_loaded = false;
46
47                 $this->load();
48         }
49
50         /**
51          * {@inheritDoc}
52          *
53          * This loads all config values everytime load is called
54          */
55         public function load(string $cat = 'config')
56         {
57                 // Don't load the whole configuration twice
58                 if ($this->config_loaded) {
59                         return;
60                 }
61
62                 // If not connected, do nothing
63                 if (!$this->configRepo->isConnected()) {
64                         return;
65                 }
66
67                 $config              = $this->configRepo->load();
68                 $this->config_loaded = true;
69
70                 // load the whole category out of the DB into the cache
71                 $this->configCache->load($config, Cache::SOURCE_DB);
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
78         {
79                 if ($refresh) {
80                         if ($this->configRepo->isConnected()) {
81                                 $config = $this->configRepo->get($cat, $key);
82                                 if (isset($config)) {
83                                         $this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
84                                 }
85                         }
86                 }
87
88                 // use the config cache for return
89                 $result = $this->configCache->get($cat, $key);
90
91                 return (isset($result)) ? $result : $default_value;
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         public function set(string $cat, string $key, $value): bool
98         {
99                 if (!$this->config_loaded) {
100                         $this->load();
101                 }
102
103                 // set the cache first
104                 $cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
105
106                 // If there is no connected adapter, we're finished
107                 if (!$this->configRepo->isConnected()) {
108                         return $cached;
109                 }
110
111                 $stored = $this->configRepo->set($cat, $key, $value);
112
113                 return $cached && $stored;
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         public function delete(string $cat, string $key): bool
120         {
121                 if ($this->config_loaded) {
122                         $this->load();
123                 }
124
125                 $cacheRemoved = $this->configCache->delete($cat, $key);
126
127                 if (!$this->configRepo->isConnected()) {
128                         return $cacheRemoved;
129                 }
130
131                 $storeRemoved = $this->configRepo->delete($cat, $key);
132
133                 return $cacheRemoved || $storeRemoved;
134         }
135 }