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