]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/Type/PreloadPConfig.php
New table "post-counts" to precalculate the counts
[friendica.git] / src / Core / PConfig / Type / PreloadPConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Type;
23
24 use Friendica\Core\PConfig\Repository;
25 use Friendica\Core\PConfig\ValueObject;
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 AbstractPConfigValues
34 {
35         const NAME = 'preload';
36
37         /** @var array */
38         private $config_loaded;
39
40         /**
41          * @param ValueObject\Cache  $configCache The configuration cache
42          * @param Repository\PConfig $configRepo  The configuration model
43          */
44         public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
45         {
46                 parent::__construct($configCache, $configRepo);
47                 $this->config_loaded = [];
48         }
49
50         /**
51          * {@inheritDoc}
52          *
53          * This loads all config values everytime load is called
54          *
55          */
56         public function load(int $uid, string $cat = 'config'): array
57         {
58                 // Don't load the whole configuration twice or with invalid uid
59                 if (!$uid || !empty($this->config_loaded[$uid])) {
60                         return [];
61                 }
62
63                 // If not connected, do nothing
64                 if (!$this->configModel->isConnected()) {
65                         return [];
66                 }
67
68                 $config                    = $this->configModel->load($uid);
69                 $this->config_loaded[$uid] = true;
70
71                 // load the whole category out of the DB into the cache
72                 $this->configCache->load($uid, $config);
73
74                 return $config;
75         }
76
77         /**
78          * {@inheritDoc}
79          */
80         public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
81         {
82                 if (!$uid) {
83                         return $default_value;
84                 }
85
86                 if (empty($this->config_loaded[$uid])) {
87                         $this->load($uid);
88                 } elseif ($refresh) {
89                         if ($this->configModel->isConnected()) {
90                                 $config = $this->configModel->get($uid, $cat, $key);
91                                 if (isset($config)) {
92                                         $this->configCache->set($uid, $cat, $key, $config);
93                                 }
94                         }
95                 }
96
97                 // use the config cache for return
98                 $result = $this->configCache->get($uid, $cat, $key);
99
100                 return (isset($result)) ? $result : $default_value;
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         public function set(int $uid, string $cat, string $key, $value): bool
107         {
108                 if (!$uid) {
109                         return false;
110                 }
111
112                 if (empty($this->config_loaded[$uid])) {
113                         $this->load($uid);
114                 }
115
116                 // set the cache first
117                 $cached = $this->configCache->set($uid, $cat, $key, $value);
118
119                 // If there is no connected adapter, we're finished
120                 if (!$this->configModel->isConnected()) {
121                         return $cached;
122                 }
123
124                 $stored = $this->configModel->set($uid, $cat, $key, $value);
125
126                 return $cached && $stored;
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         public function delete(int $uid, string $cat, string $key): bool
133         {
134                 if (!$uid) {
135                         return false;
136                 }
137
138                 if (empty($this->config_loaded[$uid])) {
139                         $this->load($uid);
140                 }
141
142                 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
143
144                 if (!$this->configModel->isConnected()) {
145                         return $cacheRemoved;
146                 }
147
148                 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
149
150                 return $cacheRemoved || $storeRemoved;
151         }
152 }