3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
6 use Friendica\Database\DBA;
7 use Friendica\Util\DateTimeFormat;
10 * Database Cache Driver
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
19 public function getAllKeys($prefix = null)
22 $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
24 $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
27 $stmt = DBA::select('cache', ['k'], $where);
30 while ($key = DBA::fetch($stmt)) {
31 array_push($keys, $key['k']);
41 public function get($key)
43 $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
45 if (DBA::isResult($cache)) {
46 $cached = $cache['v'];
47 $value = @unserialize($cached);
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) {
63 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
67 'v' => serialize($value),
68 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
69 'updated' => DateTimeFormat::utcNow()
73 'v' => serialize($value),
75 'updated' => DateTimeFormat::utcNow()
79 return DBA::update('cache', $fields, ['k' => $key], true);
85 public function delete($key)
87 return DBA::delete('cache', ['k' => $key]);
93 public function clear($outdated = true)
96 return DBA::delete('cache', ['`expires` < NOW()']);
98 return DBA::delete('cache', ['`k` IS NOT NULL ']);