]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JitConfiguration.php
wrapping up 2019.12
[friendica.git] / src / Core / Config / JitConfiguration.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Friendica\Model;
6
7 /**
8  * This class implements the Just-In-Time configuration, which will cache
9  * config values in a cache, once they are retrieved.
10  *
11  * Default Configuration type.
12  * Provides the best performance for pages loading few configuration variables.
13  */
14 class JitConfiguration extends Configuration
15 {
16         /**
17          * @var array Array of already loaded db values (even if there was no value)
18          */
19         private $db_loaded;
20
21         /**
22          * @param Cache\ConfigCache   $configCache The configuration cache (based on the config-files)
23          * @param Model\Config\Config $configModel The configuration model
24          */
25         public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
26         {
27                 parent::__construct($configCache, $configModel);
28                 $this->db_loaded = [];
29
30                 $this->load();
31         }
32
33         /**
34          * {@inheritDoc}
35          *
36          */
37         public function load(string $cat = 'config')
38         {
39                 // If not connected, do nothing
40                 if (!$this->configModel->isConnected()) {
41                         return;
42                 }
43
44                 $config = $this->configModel->load($cat);
45
46                 if (!empty($config[$cat])) {
47                         foreach ($config[$cat] as $key => $value) {
48                                 $this->db_loaded[$cat][$key] = true;
49                         }
50                 }
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 the value isn't loaded or refresh is needed, load it to the cache
62                 if ($this->configModel->isConnected() &&
63                     (empty($this->db_loaded[$cat][$key]) ||
64                      $refresh)) {
65
66                         $dbvalue = $this->configModel->get($cat, $key);
67
68                         if (isset($dbvalue)) {
69                                 $this->configCache->set($cat, $key, $dbvalue);
70                                 unset($dbvalue);
71                         }
72
73                         $this->db_loaded[$cat][$key] = true;
74                 }
75
76                 // use the config cache for return
77                 $result = $this->configCache->get($cat, $key);
78
79                 return (isset($result)) ? $result : $default_value;
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         public function set(string $cat, string $key, $value)
86         {
87                 // set the cache first
88                 $cached = $this->configCache->set($cat, $key, $value);
89
90                 // If there is no connected adapter, we're finished
91                 if (!$this->configModel->isConnected()) {
92                         return $cached;
93                 }
94
95                 $stored = $this->configModel->set($cat, $key, $value);
96
97                 $this->db_loaded[$cat][$key] = $stored;
98
99                 return $cached && $stored;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public function delete(string $cat, string $key)
106         {
107                 $cacheRemoved = $this->configCache->delete($cat, $key);
108
109                 if (isset($this->db_loaded[$cat][$key])) {
110                         unset($this->db_loaded[$cat][$key]);
111                 }
112
113                 if (!$this->configModel->isConnected()) {
114                         return $cacheRemoved;
115                 }
116
117                 $storeRemoved = $this->configModel->delete($cat, $key);
118
119                 return $cacheRemoved || $storeRemoved;
120         }
121 }