]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadConfig.php
Remove duplicate profile_uid key in App->profile array
[friendica.git] / src / Core / Config / PreloadConfig.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Friendica\Core\BaseConfig;
6 use Friendica\Model;
7
8 /**
9  * This class implements the preload configuration, which will cache
10  * all config values per call in a cache.
11  *
12  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
13  */
14 class PreloadConfig extends BaseConfig
15 {
16         /** @var bool */
17         private $config_loaded;
18
19         /**
20          * @param Cache               $configCache The configuration cache (based on the config-files)
21          * @param Model\Config\Config $configModel The configuration model
22          */
23         public function __construct(Cache $configCache, Model\Config\Config $configModel)
24         {
25                 parent::__construct($configCache, $configModel);
26                 $this->config_loaded = false;
27
28                 $this->load();
29         }
30
31         /**
32          * {@inheritDoc}
33          *
34          * This loads all config values everytime load is called
35          *
36          */
37         public function load(string $cat = 'config')
38         {
39                 // Don't load the whole configuration twice
40                 if ($this->config_loaded) {
41                         return;
42                 }
43
44                 // If not connected, do nothing
45                 if (!$this->configModel->isConnected()) {
46                         return;
47                 }
48
49                 $config              = $this->configModel->load();
50                 $this->config_loaded = true;
51
52                 // load the whole category out of the DB into the cache
53                 $this->configCache->load($config, true);
54         }
55
56         /**
57          * {@inheritDoc}
58          */
59         public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
60         {
61                 if ($refresh) {
62                         if ($this->configModel->isConnected()) {
63                                 $config = $this->configModel->get($cat, $key);
64                                 if (isset($config)) {
65                                         $this->configCache->set($cat, $key, $config);
66                                 }
67                         }
68                 }
69
70                 // use the config cache for return
71                 $result = $this->configCache->get($cat, $key);
72
73                 return (isset($result)) ? $result : $default_value;
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         public function set(string $cat, string $key, $value)
80         {
81                 if (!$this->config_loaded) {
82                         $this->load();
83                 }
84
85                 // set the cache first
86                 $cached = $this->configCache->set($cat, $key, $value);
87
88                 // If there is no connected adapter, we're finished
89                 if (!$this->configModel->isConnected()) {
90                         return $cached;
91                 }
92
93                 $stored = $this->configModel->set($cat, $key, $value);
94
95                 return $cached && $stored;
96         }
97
98         /**
99          * {@inheritDoc}
100          */
101         public function delete(string $cat, string $key)
102         {
103                 if ($this->config_loaded) {
104                         $this->load();
105                 }
106
107                 $cacheRemoved = $this->configCache->delete($cat, $key);
108
109                 if (!$this->configModel->isConnected()) {
110                         return $cacheRemoved;
111                 }
112
113                 $storeRemoved = $this->configModel->delete($cat, $key);
114
115                 return $cacheRemoved || $storeRemoved;
116         }
117 }