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