]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/JitPConfiguration.php
8ad65eff1a93a01f7c1e43d2f478c26ef44814fc
[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, do nothing
38                 if (!$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 the value isn't loaded or refresh is needed, load it to the cache
60                 if ($this->configModel->isConnected() &&
61                     (empty($this->db_loaded[$uid][$cat][$key]) ||
62                      $refresh)) {
63
64                         $dbvalue = $this->configModel->get($uid, $cat, $key);
65
66                         if (isset($dbvalue)) {
67                                 $this->configCache->set($uid, $cat, $key, $dbvalue);
68                                 unset($dbvalue);
69                         }
70
71                         $this->db_loaded[$uid][$cat][$key] = true;
72                 }
73
74                 // use the config cache for return
75                 $result = $this->configCache->get($uid, $cat, $key);
76
77                 return (isset($result)) ? $result : $default_value;
78         }
79
80         /**
81          * {@inheritDoc}
82          */
83         public function set(int $uid, string $cat, string $key, $value)
84         {
85                 // set the cache first
86                 $cached = $this->configCache->set($uid, $cat, $key, $value);
87
88                 // If there is no connected adapter, we're finished
89                 if (!$this->configModel->isConnected()) {
90                         return $cached;
91                 }
92
93                 $stored = $this->configModel->set($uid, $cat, $key, $value);
94
95                 $this->db_loaded[$uid][$cat][$key] = $stored;
96
97                 return $cached && $stored;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         public function delete(int $uid, string $cat, string $key)
104         {
105                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
106
107                 if (isset($this->db_loaded[$uid][$cat][$key])) {
108                         unset($this->db_loaded[$uid][$cat][$key]);
109                 }
110
111                 if (!$this->configModel->isConnected()) {
112                         return $cacheRemoved;
113                 }
114
115                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
116
117                 return $cacheRemoved || $storeRemoved;
118         }
119 }