2018-03-24 18:39:13 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
2019-08-03 18:48:56 +00:00
|
|
|
use Friendica\Database\Database;
|
2018-03-24 18:39:13 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2020-01-18 14:41:19 +00:00
|
|
|
use Friendica\Core\BaseCache;
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-04 08:26:53 +00:00
|
|
|
* Database Cache
|
2018-03-24 18:39:13 +00:00
|
|
|
*/
|
2020-01-18 14:41:19 +00:00
|
|
|
class DatabaseCache extends BaseCache implements ICache
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2019-08-03 18:48:56 +00:00
|
|
|
/**
|
|
|
|
* @var Database
|
|
|
|
*/
|
|
|
|
private $dba;
|
|
|
|
|
|
|
|
public function __construct(string $hostname, Database $dba)
|
|
|
|
{
|
|
|
|
parent::__construct($hostname);
|
|
|
|
|
|
|
|
$this->dba = $dba;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-08-03 18:48:56 +00:00
|
|
|
$stmt = $this->dba->select('cache', ['k'], $where);
|
2018-10-06 22:27:54 +00:00
|
|
|
|
2018-10-07 20:14:05 +00:00
|
|
|
$keys = [];
|
2019-08-03 18:48:56 +00:00
|
|
|
while ($key = $this->dba->fetch($stmt)) {
|
2018-10-07 20:14:05 +00:00
|
|
|
array_push($keys, $key['k']);
|
2018-10-06 22:27:54 +00:00
|
|
|
}
|
2019-08-03 18:48:56 +00:00
|
|
|
$this->dba->close($stmt);
|
2018-10-06 22:27:54 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2019-08-03 18:48:56 +00:00
|
|
|
$cache = $this->dba->selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2019-08-03 18:48:56 +00:00
|
|
|
if ($this->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)
|
|
|
|
*/
|
2020-01-18 14:41:19 +00:00
|
|
|
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2018-10-29 09:16:07 +00: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 18:39:13 +00:00
|
|
|
|
2019-08-03 18:48:56 +00:00
|
|
|
return $this->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)
|
|
|
|
{
|
2019-08-03 18:48:56 +00:00
|
|
|
return $this->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) {
|
2019-08-03 18:48:56 +00:00
|
|
|
return $this->dba->delete('cache', ['`expires` < NOW()']);
|
2018-07-07 17:46:16 +00:00
|
|
|
} else {
|
2019-08-03 18:48:56 +00:00
|
|
|
return $this->dba->delete('cache', ['`k` IS NOT NULL ']);
|
2018-07-07 17:46:16 +00:00
|
|
|
}
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
2019-08-04 13:42:39 +00:00
|
|
|
|
2019-08-04 14:13:53 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
2019-08-04 13:42:39 +00:00
|
|
|
{
|
2020-01-18 14:41:19 +00:00
|
|
|
return Type::DATABASE;
|
2019-08-04 13:42:39 +00:00
|
|
|
}
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|