]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/DatabaseCache.php
Merge pull request #8996 from MrPetovan/bug/8995-profile-contacts-is-owner
[friendica.git] / src / Core / Cache / DatabaseCache.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\Cache;
23
24 use Friendica\Database\Database;
25 use Friendica\Util\DateTimeFormat;
26 use Friendica\Core\BaseCache;
27
28 /**
29  * Database Cache
30  */
31 class DatabaseCache extends BaseCache implements ICache
32 {
33         /**
34          * @var Database
35          */
36         private $dba;
37
38         public function __construct(string $hostname, Database $dba)
39         {
40                 parent::__construct($hostname);
41
42                 $this->dba = $dba;
43         }
44
45         /**
46          * (@inheritdoc)
47          */
48         public function getAllKeys($prefix = null)
49         {
50                 if (empty($prefix)) {
51                         $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
52                 } else {
53                         $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
54                 }
55
56                 $stmt = $this->dba->select('cache', ['k'], $where);
57
58                 $keys = [];
59                 while ($key = $this->dba->fetch($stmt)) {
60                         array_push($keys, $key['k']);
61                 }
62                 $this->dba->close($stmt);
63
64                 return $keys;
65         }
66
67         /**
68          * (@inheritdoc)
69          */
70         public function get($key)
71         {
72                 $cache = $this->dba->selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
73
74                 if ($this->dba->isResult($cache)) {
75                         $cached = $cache['v'];
76                         $value = @unserialize($cached);
77
78                         // Only return a value if the serialized value is valid.
79                         // We also check if the db entry is a serialized
80                         // boolean 'false' value (which we want to return).
81                         if ($cached === serialize(false) || $value !== false) {
82                                 return $value;
83                         }
84                 }
85
86                 return null;
87         }
88
89         /**
90          * (@inheritdoc)
91          */
92         public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
93         {
94                 if ($ttl > 0) {
95                         $fields = [
96                                 'v' => serialize($value),
97                                 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
98                                 'updated' => DateTimeFormat::utcNow()
99                         ];
100                 } else {
101                         $fields = [
102                                 'v' => serialize($value),
103                                 'expires' => -1,
104                                 'updated' => DateTimeFormat::utcNow()
105                         ];
106                 }
107
108                 return $this->dba->update('cache', $fields, ['k' => $key], true);
109         }
110
111         /**
112          * (@inheritdoc)
113          */
114         public function delete($key)
115         {
116                 return $this->dba->delete('cache', ['k' => $key]);
117         }
118
119         /**
120          * (@inheritdoc)
121          */
122         public function clear($outdated = true)
123         {
124                 if ($outdated) {
125                         return $this->dba->delete('cache', ['`expires` < NOW()']);
126                 } else {
127                         return $this->dba->delete('cache', ['`k` IS NOT NULL ']);
128                 }
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         public function getName()
135         {
136                 return Type::DATABASE;
137         }
138 }