]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/PreloadPConfig.php
Merge pull request #9039 from MrPetovan/task/frio-accent-scheme
[friendica.git] / src / Core / PConfig / PreloadPConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\PConfig;
23
24 use Friendica\Core\BasePConfig;
25 use Friendica\Model;
26
27 /**
28  * This class implements the preload configuration, which will cache
29  * all user config values per call in a cache.
30  *
31  * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
32  */
33 class PreloadPConfig extends BasePConfig
34 {
35         /** @var array */
36         private $config_loaded;
37
38         /**
39          * @param Cache                $configCache The configuration cache
40          * @param Model\Config\PConfig $configModel The configuration model
41          */
42         public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
43         {
44                 parent::__construct($configCache, $configModel);
45                 $this->config_loaded = [];
46         }
47
48         /**
49          * {@inheritDoc}
50          *
51          * This loads all config values everytime load is called
52          *
53          */
54         public function load(int $uid, string $cat = 'config')
55         {
56                 // Don't load the whole configuration twice or with invalid uid
57                 if (!$uid || !empty($this->config_loaded[$uid])) {
58                         return;
59                 }
60
61                 // If not connected, do nothing
62                 if (!$this->configModel->isConnected()) {
63                         return;
64                 }
65
66                 $config                    = $this->configModel->load($uid);
67                 $this->config_loaded[$uid] = true;
68
69                 // load the whole category out of the DB into the cache
70                 $this->configCache->load($uid, $config);
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
77         {
78                 if (!$uid) {
79                         return $default_value;
80                 }
81
82                 if (empty($this->config_loaded[$uid])) {
83                         $this->load($uid);
84                 } elseif ($refresh) {
85                         if ($this->configModel->isConnected()) {
86                                 $config = $this->configModel->get($uid, $cat, $key);
87                                 if (isset($config)) {
88                                         $this->configCache->set($uid, $cat, $key, $config);
89                                 }
90                         }
91                 }
92
93                 // use the config cache for return
94                 $result = $this->configCache->get($uid, $cat, $key);
95
96                 return (isset($result)) ? $result : $default_value;
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         public function set(int $uid, string $cat, string $key, $value)
103         {
104                 if (!$uid) {
105                         return false;
106                 }
107
108                 if (empty($this->config_loaded[$uid])) {
109                         $this->load($uid);
110                 }
111
112                 // set the cache first
113                 $cached = $this->configCache->set($uid, $cat, $key, $value);
114
115                 // If there is no connected adapter, we're finished
116                 if (!$this->configModel->isConnected()) {
117                         return $cached;
118                 }
119
120                 $stored = $this->configModel->set($uid, $cat, $key, $value);
121
122                 return $cached && $stored;
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         public function delete(int $uid, string $cat, string $key)
129         {
130                 if (!$uid) {
131                         return false;
132                 }
133
134                 if (empty($this->config_loaded[$uid])) {
135                         $this->load($uid);
136                 }
137
138                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
139
140                 if (!$this->configModel->isConnected()) {
141                         return $cacheRemoved;
142                 }
143
144                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
145
146                 return $cacheRemoved || $storeRemoved;
147         }
148 }