]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/DatabaseCacheDriver.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git] / src / Core / Cache / DatabaseCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6 use Friendica\Database\DBA;
7 use Friendica\Util\DateTimeFormat;
8
9 /**
10  * Database Cache Driver
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
15 {
16         /**
17          * (@inheritdoc)
18          */
19         public function getAllKeys($prefix = null)
20         {
21                 if (empty($prefix)) {
22                         $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
23                 } else {
24                         $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
25                 }
26
27                 $stmt = DBA::select('cache', ['k'], $where);
28
29                 $keys = [];
30                 while ($key = DBA::fetch($stmt)) {
31                         array_push($keys, $key['k']);
32                 }
33                 DBA::close($stmt);
34
35                 return $keys;
36         }
37
38         /**
39          * (@inheritdoc)
40          */
41         public function get($key)
42         {
43                 $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
44
45                 if (DBA::isResult($cache)) {
46                         $cached = $cache['v'];
47                         $value = @unserialize($cached);
48
49                         // Only return a value if the serialized value is valid.
50                         // We also check if the db entry is a serialized
51                         // boolean 'false' value (which we want to return).
52                         if ($cached === serialize(false) || $value !== false) {
53                                 return $value;
54                         }
55                 }
56
57                 return null;
58         }
59
60         /**
61          * (@inheritdoc)
62          */
63         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
64         {
65                 $fields = [
66                         'v'       => serialize($value),
67                         'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
68                         'updated' => DateTimeFormat::utcNow()
69                 ];
70
71                 return DBA::update('cache', $fields, ['k' => $key], true);
72         }
73
74         /**
75          * (@inheritdoc)
76          */
77         public function delete($key)
78         {
79                 return DBA::delete('cache', ['k' => $key]);
80         }
81
82         /**
83          * (@inheritdoc)
84          */
85         public function clear($outdated = true)
86         {
87                 if ($outdated) {
88                         return DBA::delete('cache', ['`expires` < NOW()']);
89                 } else {
90                         return DBA::delete('cache', ['`k` IS NOT NULL ']);
91                 }
92         }
93 }