1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-08-06 16:46:30 +02:00

refactor(cache): extract and encapsulate cache expiration logic (#3547)

* refactor(cache): extract and encapsulate cache expiration logic

* fix: logic bug in getSimpleHTMLDOMCached

* fix: silly me, index should of course be on the key column

* silly me again, PRIMARY keys get index by default lol

* comment out the delete portion in loadData

* remove a few log statements

* tweak twitter cache timeout
This commit is contained in:
Dag
2023-07-19 05:05:49 +02:00
committed by GitHub
parent 087e790ec1
commit 6254b8593e
12 changed files with 124 additions and 137 deletions

View File

@@ -1,5 +1,8 @@
<?php
/**
* @link https://www.php.net/manual/en/function.clearstatcache.php
*/
class FileCache implements CacheInterface
{
private array $config;
@@ -25,18 +28,25 @@ class FileCache implements CacheInterface
return $this->config;
}
public function loadData()
public function loadData(int $timeout = 86400)
{
clearstatcache();
if (!file_exists($this->getCacheFile())) {
return null;
}
$data = unserialize(file_get_contents($this->getCacheFile()));
if ($data === false) {
// Intentionally not throwing an exception
Logger::warning(sprintf('Failed to unserialize: %s', $this->getCacheFile()));
return null;
$modificationTime = filemtime($this->getCacheFile());
if (time() - $timeout < $modificationTime) {
$data = unserialize(file_get_contents($this->getCacheFile()));
if ($data === false) {
Logger::warning(sprintf('Failed to unserialize: %s', $this->getCacheFile()));
// Intentionally not throwing an exception
return null;
}
return $data;
}
return $data;
// It's a good idea to delete the expired item here, but commented out atm
// unlink($this->getCacheFile());
return null;
}
public function saveData($data): void
@@ -49,9 +59,7 @@ class FileCache implements CacheInterface
public function getTime(): ?int
{
// https://www.php.net/manual/en/function.clearstatcache.php
clearstatcache();
$cacheFile = $this->getCacheFile();
if (file_exists($cacheFile)) {
$time = filemtime($cacheFile);
@@ -64,7 +72,7 @@ class FileCache implements CacheInterface
return null;
}
public function purgeCache(int $seconds): void
public function purgeCache(int $timeout = 86400): void
{
if (! $this->config['enable_purge']) {
return;
@@ -90,7 +98,7 @@ class FileCache implements CacheInterface
continue;
} elseif ($cacheFile->isFile()) {
$filepath = $cacheFile->getPathname();
if (filemtime($filepath) < time() - $seconds) {
if (filemtime($filepath) < time() - $timeout) {
// todo: sometimes this file doesn't exists
unlink($filepath);
}

View File

@@ -6,8 +6,6 @@ class MemcachedCache implements CacheInterface
private string $key;
private $conn;
private $expiration = 0;
private $time = null;
private $data = null;
public function __construct()
{
@@ -43,50 +41,53 @@ class MemcachedCache implements CacheInterface
$this->conn = $conn;
}
public function loadData()
public function loadData(int $timeout = 86400)
{
if ($this->data) {
return $this->data;
}
$result = $this->conn->get($this->getCacheKey());
if ($result === false) {
$value = $this->conn->get($this->getCacheKey());
if ($value === false) {
return null;
}
$this->time = $result['time'];
$this->data = $result['data'];
return $result['data'];
if (time() - $timeout < $value['time']) {
return $value['data'];
}
return null;
}
public function saveData($data): void
{
$time = time();
$object_to_save = [
$value = [
'data' => $data,
'time' => $time,
'time' => time(),
];
$result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
$result = $this->conn->set($this->getCacheKey(), $value, $this->expiration);
if ($result === false) {
throw new \Exception('Cannot write the cache to memcached server');
Logger::warning('Failed to store an item in memcached', [
'scope' => $this->scope,
'key' => $this->key,
'expiration' => $this->expiration,
'code' => $this->conn->getLastErrorCode(),
'message' => $this->conn->getLastErrorMessage(),
'number' => $this->conn->getLastErrorErrno(),
]);
// Intentionally not throwing an exception
}
$this->time = $time;
}
public function getTime(): ?int
{
if ($this->time === null) {
$this->loadData();
$value = $this->conn->get($this->getCacheKey());
if ($value === false) {
return null;
}
return $this->time;
return $value['time'];
}
public function purgeCache(int $seconds): void
public function purgeCache(int $timeout = 86400): void
{
$this->conn->flush();
// Note: does not purges cache right now
// Just sets cache expiration and leave cache purging for memcached itself
$this->expiration = $seconds;
$this->expiration = $timeout;
}
public function setScope(string $scope): void

View File

@@ -12,7 +12,7 @@ class NullCache implements CacheInterface
{
}
public function loadData()
public function loadData(int $timeout = 86400)
{
}
@@ -25,7 +25,7 @@ class NullCache implements CacheInterface
return null;
}
public function purgeCache(int $seconds): void
public function purgeCache(int $timeout = 86400): void
{
}
}

View File

@@ -29,27 +29,37 @@ class SQLiteCache implements CacheInterface
$this->db = new \SQLite3($config['file']);
$this->db->enableExceptions(true);
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
$this->db->exec('CREATE INDEX idx_storage_updated ON storage (updated)');
}
$this->db->busyTimeout($config['timeout']);
}
public function loadData()
public function loadData(int $timeout = 86400)
{
$stmt = $this->db->prepare('SELECT value FROM storage WHERE key = :key');
$stmt = $this->db->prepare('SELECT value, updated FROM storage WHERE key = :key');
$stmt->bindValue(':key', $this->getCacheKey());
$result = $stmt->execute();
if ($result) {
$row = $result->fetchArray(\SQLITE3_ASSOC);
if ($row !== false) {
$blob = $row['value'];
$data = unserialize($blob);
if ($data !== false) {
return $data;
}
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
}
if (!$result) {
return null;
}
$row = $result->fetchArray(\SQLITE3_ASSOC);
if ($row === false) {
return null;
}
$value = $row['value'];
$modificationTime = $row['updated'];
if (time() - $timeout < $modificationTime) {
$data = unserialize($value);
if ($data === false) {
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($value, 0, 100)));
return null;
}
return $data;
}
// It's a good idea to delete expired cache items.
// However I'm seeing lots of SQLITE_BUSY errors so commented out for now
// $stmt = $this->db->prepare('DELETE FROM storage WHERE key = :key');
// $stmt->bindValue(':key', $this->getCacheKey());
// $stmt->execute();
return null;
}
@@ -78,13 +88,13 @@ class SQLiteCache implements CacheInterface
return null;
}
public function purgeCache(int $seconds): void
public function purgeCache(int $timeout = 86400): void
{
if (!$this->config['enable_purge']) {
return;
}
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
$stmt->bindValue(':expired', time() - $seconds);
$stmt->bindValue(':expired', time() - $timeout);
$stmt->execute();
}