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