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