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