]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/PreloadPConfig.php
Remove duplicate profile_uid key in App->profile array
[friendica.git] / src / Core / PConfig / PreloadPConfig.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 preload configuration, which will cache
10  * all user config values per call in a cache.
11  *
12  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
13  */
14 class PreloadPConfig extends BasePConfig
15 {
16         /** @var array */
17         private $config_loaded;
18
19         /**
20          * @param Cache                $configCache The configuration cache
21          * @param Model\Config\PConfig $configModel The configuration model
22          */
23         public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
24         {
25                 parent::__construct($configCache, $configModel);
26                 $this->config_loaded = [];
27         }
28
29         /**
30          * {@inheritDoc}
31          *
32          * This loads all config values everytime load is called
33          *
34          */
35         public function load(int $uid, string $cat = 'config')
36         {
37                 // Don't load the whole configuration twice or with invalid uid
38                 if (!$uid || !empty($this->config_loaded[$uid])) {
39                         return;
40                 }
41
42                 // If not connected, do nothing
43                 if (!$this->configModel->isConnected()) {
44                         return;
45                 }
46
47                 $config                    = $this->configModel->load($uid);
48                 $this->config_loaded[$uid] = true;
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 (empty($this->config_loaded[$uid])) {
64                         $this->load($uid);
65                 } elseif ($refresh) {
66                         if ($this->configModel->isConnected()) {
67                                 $config = $this->configModel->get($uid, $cat, $key);
68                                 if (isset($config)) {
69                                         $this->configCache->set($uid, $cat, $key, $config);
70                                 }
71                         }
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                 if (!$uid) {
86                         return false;
87                 }
88
89                 if (empty($this->config_loaded[$uid])) {
90                         $this->load($uid);
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                 return $cached && $stored;
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         public function delete(int $uid, string $cat, string $key)
110         {
111                 if (!$uid) {
112                         return false;
113                 }
114
115                 if (empty($this->config_loaded[$uid])) {
116                         $this->load($uid);
117                 }
118
119                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
120
121                 if (!$this->configModel->isConnected()) {
122                         return $cacheRemoved;
123                 }
124
125                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
126
127                 return $cacheRemoved || $storeRemoved;
128         }
129 }