X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FCore%2FCache%2FDatabaseCacheDriver.php;h=f6f5b6486cb6b7c3d02d4af991a84cc955758da5;hb=fa1f48e1711c64d42ddb0f72c7670fa28f56482d;hp=9703208d7b48888024f4082b5417630a51332ca5;hpb=3df34d33d5fbc4b7599cb1df09c0af4648f4f7c7;p=friendica.git diff --git a/src/Core/Cache/DatabaseCacheDriver.php b/src/Core/Cache/DatabaseCacheDriver.php index 9703208d7b..f6f5b6486c 100644 --- a/src/Core/Cache/DatabaseCacheDriver.php +++ b/src/Core/Cache/DatabaseCacheDriver.php @@ -1,56 +1,101 @@ - - */ -class DatabaseCacheDriver implements ICacheDriver -{ - public function get($key) - { - $cache = dba::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]); - - if (DBM::is_result($cache)) { - $cached = $cache['v']; - $value = @unserialize($cached); - - // Only return a value if the serialized value is valid. - // We also check if the db entry is a serialized - // boolean 'false' value (which we want to return). - if ($cached === serialize(false) || $value !== false) { - return $value; - } - } - - return null; - } - - public function set($key, $value, $duration = Cache::MONTH) - { - $fields = [ - 'v' => serialize($value), - 'expires' => DateTimeFormat::utc('now + ' . Cache::duration($duration) . ' seconds'), - 'updated' => DateTimeFormat::utcNow() - ]; - - return dba::update('cache', $fields, ['k' => $key], true); - } - - public function delete($key) - { - return dba::delete('cache', ['k' => $key]); - } - - public function clear() - { - return dba::delete('cache', ['`expires` < NOW()']); - } -} + + */ +class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver +{ + /** + * (@inheritdoc) + */ + public function getAllKeys($prefix = null) + { + if (empty($prefix)) { + $where = ['`expires` >= ?', DateTimeFormat::utcNow()]; + } else { + $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix]; + } + + $stmt = DBA::select('cache', ['k'], $where); + + $keys = []; + while ($key = DBA::fetch($stmt)) { + array_push($keys, $key['k']); + } + DBA::close($stmt); + + return $keys; + } + + /** + * (@inheritdoc) + */ + public function get($key) + { + $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]); + + if (DBA::isResult($cache)) { + $cached = $cache['v']; + $value = @unserialize($cached); + + // Only return a value if the serialized value is valid. + // We also check if the db entry is a serialized + // boolean 'false' value (which we want to return). + if ($cached === serialize(false) || $value !== false) { + return $value; + } + } + + return null; + } + + /** + * (@inheritdoc) + */ + public function set($key, $value, $ttl = Cache::FIVE_MINUTES) + { + if ($ttl > 0) { + $fields = [ + 'v' => serialize($value), + 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'), + 'updated' => DateTimeFormat::utcNow() + ]; + } else { + $fields = [ + 'v' => serialize($value), + 'expires' => -1, + 'updated' => DateTimeFormat::utcNow() + ]; + } + + return DBA::update('cache', $fields, ['k' => $key], true); + } + + /** + * (@inheritdoc) + */ + public function delete($key) + { + return DBA::delete('cache', ['k' => $key]); + } + + /** + * (@inheritdoc) + */ + public function clear($outdated = true) + { + if ($outdated) { + return DBA::delete('cache', ['`expires` < NOW()']); + } else { + return DBA::delete('cache', ['`k` IS NOT NULL ']); + } + } +}