]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/PreloadPConfiguration.php
Merge pull request #7754 from annando/aria
[friendica.git] / src / Core / Config / PreloadPConfiguration.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 use Friendica\Model;
6
7 /**
8  * This class implements the preload configuration, which will cache
9  * all user config values per call in a cache.
10  *
11  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
12  */
13 class PreloadPConfiguration extends PConfiguration
14 {
15         /** @var array */
16         private $config_loaded;
17
18         /**
19          * @param Cache\PConfigCache   $configCache The configuration cache
20          * @param Model\Config\PConfig $configModel The configuration model
21          */
22         public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
23         {
24                 parent::__construct($configCache, $configModel);
25                 $this->config_loaded = [];
26         }
27
28         /**
29          * {@inheritDoc}
30          *
31          * This loads all config values everytime load is called
32          *
33          */
34         public function load(int $uid, string $cat = 'config')
35         {
36                 // Don't load the whole configuration twice or with invalid uid
37                 if (!$uid || !empty($this->config_loaded[$uid])) {
38                         return;
39                 }
40
41                 // If not connected, do nothing
42                 if (!$this->configModel->isConnected()) {
43                         return;
44                 }
45
46                 $config                    = $this->configModel->load($uid);
47                 $this->config_loaded[$uid] = true;
48
49                 // load the whole category out of the DB into the cache
50                 $this->configCache->load($uid, $config);
51         }
52
53         /**
54          * {@inheritDoc}
55          */
56         public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
57         {
58                 if (!$uid) {
59                         return $default_value;
60                 }
61
62                 if (empty($this->config_loaded[$uid])) {
63                         $this->load($uid);
64                 } elseif ($refresh) {
65                         if ($this->configModel->isConnected()) {
66                                 $config = $this->configModel->get($uid, $cat, $key);
67                                 if (isset($config)) {
68                                         $this->configCache->set($uid, $cat, $key, $config);
69                                 }
70                         }
71                 }
72
73                 // use the config cache for return
74                 $result = $this->configCache->get($uid, $cat, $key);
75
76                 return (isset($result)) ? $result : $default_value;
77         }
78
79         /**
80          * {@inheritDoc}
81          */
82         public function set(int $uid, string $cat, string $key, $value)
83         {
84                 if (!$uid) {
85                         return false;
86                 }
87
88                 if (empty($this->config_loaded[$uid])) {
89                         $this->load($uid);
90                 }
91
92                 // set the cache first
93                 $cached = $this->configCache->set($uid, $cat, $key, $value);
94
95                 // If there is no connected adapter, we're finished
96                 if (!$this->configModel->isConnected()) {
97                         return $cached;
98                 }
99
100                 $stored = $this->configModel->set($uid, $cat, $key, $value);
101
102                 return $cached && $stored;
103         }
104
105         /**
106          * {@inheritDoc}
107          */
108         public function delete(int $uid, string $cat, string $key)
109         {
110                 if (!$uid) {
111                         return false;
112                 }
113
114                 if (empty($this->config_loaded[$uid])) {
115                         $this->load($uid);
116                 }
117
118                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
119
120                 if (!$this->configModel->isConnected()) {
121                         return $cacheRemoved;
122                 }
123
124                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
125
126                 return $cacheRemoved || $storeRemoved;
127         }
128 }