]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/JitPConfig.php
Merge pull request #8142 from nupplaphil/task/di_config
[friendica.git] / src / Core / PConfig / JitPConfig.php
1 <?php
2
3 namespace Friendica\Core\PConfig;
4
5 use Friendica\Core\BasePConfig;
6 use Friendica\Model;
7
8 /**
9  * This class implements the Just-In-Time configuration, which will cache
10  * user 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 JitPConfig extends BasePConfig
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
24          * @param Model\Config\PConfig $configModel The configuration model
25          */
26         public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
27         {
28                 parent::__construct($configCache, $configModel);
29                 $this->db_loaded = [];
30         }
31
32         /**
33          * {@inheritDoc}
34          *
35          */
36         public function load(int $uid, string $cat = 'config')
37         {
38                 // If not connected or no uid, do nothing
39                 if (!$uid || !$this->configModel->isConnected()) {
40                         return;
41                 }
42
43                 $config = $this->configModel->load($uid, $cat);
44
45                 if (!empty($config[$cat])) {
46                         foreach ($config[$cat] as $key => $value) {
47                                 $this->db_loaded[$uid][$cat][$key] = true;
48                         }
49                 }
50
51                 // load the whole category out of the DB into the cache
52                 $this->configCache->load($uid, $config);
53         }
54
55         /**
56          * {@inheritDoc}
57          */
58         public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
59         {
60                 if (!$uid) {
61                         return $default_value;
62                 }
63
64                 // if the value isn't loaded or refresh is needed, load it to the cache
65                 if ($this->configModel->isConnected() &&
66                     (empty($this->db_loaded[$uid][$cat][$key]) ||
67                      $refresh)) {
68
69                         $dbvalue = $this->configModel->get($uid, $cat, $key);
70
71                         if (isset($dbvalue)) {
72                                 $this->configCache->set($uid, $cat, $key, $dbvalue);
73                                 unset($dbvalue);
74                         }
75
76                         $this->db_loaded[$uid][$cat][$key] = true;
77                 }
78
79                 // use the config cache for return
80                 $result = $this->configCache->get($uid, $cat, $key);
81
82                 return (isset($result)) ? $result : $default_value;
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         public function set(int $uid, string $cat, string $key, $value)
89         {
90                 if (!$uid) {
91                         return false;
92                 }
93
94                 // set the cache first
95                 $cached = $this->configCache->set($uid, $cat, $key, $value);
96
97                 // If there is no connected adapter, we're finished
98                 if (!$this->configModel->isConnected()) {
99                         return $cached;
100                 }
101
102                 $stored = $this->configModel->set($uid, $cat, $key, $value);
103
104                 $this->db_loaded[$uid][$cat][$key] = $stored;
105
106                 return $cached && $stored;
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         public function delete(int $uid, string $cat, string $key)
113         {
114                 if (!$uid) {
115                         return false;
116                 }
117
118                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
119
120                 if (isset($this->db_loaded[$uid][$cat][$key])) {
121                         unset($this->db_loaded[$uid][$cat][$key]);
122                 }
123
124                 if (!$this->configModel->isConnected()) {
125                         return $cacheRemoved;
126                 }
127
128                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
129
130                 return $cacheRemoved || $storeRemoved;
131         }
132 }