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()
21 $stmt = DBA::select('cache', ['k'], ['`expires` >= ?', DateTimeFormat::utcNow()]);
23 return DBA::toArray($stmt);
29 public function get($key)
31 $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
33 if (DBA::isResult($cache)) {
34 $cached = $cache['v'];
35 $value = @unserialize($cached);
37 // Only return a value if the serialized value is valid.
38 // We also check if the db entry is a serialized
39 // boolean 'false' value (which we want to return).
40 if ($cached === serialize(false) || $value !== false) {
51 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
54 'v' => serialize($value),
55 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
56 'updated' => DateTimeFormat::utcNow()
59 return DBA::update('cache', $fields, ['k' => $key], true);
65 public function delete($key)
67 return DBA::delete('cache', ['k' => $key]);
73 public function clear($outdated = true)
76 return DBA::delete('cache', ['`expires` < NOW()']);
78 return DBA::delete('cache', ['`k` IS NOT NULL ']);