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