2018-03-24 18:39:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
|
|
|
use Friendica\Core\Cache;
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
2018-03-24 18:39:13 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database Cache Driver
|
|
|
|
*
|
2018-09-15 23:28:38 +00:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-03-24 18:39:13 +00:00
|
|
|
*/
|
2018-07-05 19:54:20 +00:00
|
|
|
class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2018-09-26 02:52:32 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-10-06 22:27:54 +00:00
|
|
|
public function getAllKeys($prefix = null)
|
2018-09-26 02:52:32 +00:00
|
|
|
{
|
2018-10-06 22:27:54 +00:00
|
|
|
if (empty($prefix)) {
|
|
|
|
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
|
|
|
} else {
|
2018-10-07 20:14:05 +00:00
|
|
|
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
2018-10-06 22:27:54 +00:00
|
|
|
}
|
2018-09-26 02:52:32 +00:00
|
|
|
|
2018-10-06 22:27:54 +00:00
|
|
|
$stmt = DBA::select('cache', ['k'], $where);
|
|
|
|
|
2018-10-07 20:14:05 +00:00
|
|
|
$keys = [];
|
2018-10-06 22:27:54 +00:00
|
|
|
while ($key = DBA::fetch($stmt)) {
|
2018-10-07 20:14:05 +00:00
|
|
|
array_push($keys, $key['k']);
|
2018-10-06 22:27:54 +00:00
|
|
|
}
|
|
|
|
DBA::close($stmt);
|
|
|
|
|
2018-10-07 20:14:05 +00:00
|
|
|
return $keys;
|
2018-09-26 02:52:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 02:51:41 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 18:39:13 +00:00
|
|
|
public function get($key)
|
|
|
|
{
|
2018-07-20 12:19:26 +00:00
|
|
|
$cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (DBA::isResult($cache)) {
|
2018-03-24 18:39:13 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-09-26 02:51:41 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-07-04 21:37:22 +00:00
|
|
|
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
|
|
|
$fields = [
|
|
|
|
'v' => serialize($value),
|
2018-07-07 17:46:16 +00:00
|
|
|
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
|
2018-03-24 18:39:13 +00:00
|
|
|
'updated' => DateTimeFormat::utcNow()
|
|
|
|
];
|
|
|
|
|
2018-07-20 12:19:26 +00:00
|
|
|
return DBA::update('cache', $fields, ['k' => $key], true);
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 02:51:41 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 18:39:13 +00:00
|
|
|
public function delete($key)
|
|
|
|
{
|
2018-07-20 12:19:26 +00:00
|
|
|
return DBA::delete('cache', ['k' => $key]);
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 02:51:41 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-07-07 17:46:16 +00:00
|
|
|
public function clear($outdated = true)
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2018-07-07 17:46:16 +00:00
|
|
|
if ($outdated) {
|
2018-07-20 12:19:26 +00:00
|
|
|
return DBA::delete('cache', ['`expires` < NOW()']);
|
2018-07-07 17:46:16 +00:00
|
|
|
} else {
|
2018-07-20 12:19:26 +00:00
|
|
|
return DBA::delete('cache', ['`k` IS NOT NULL ']);
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
}
|