mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-30 21:30:14 +02:00
fix: rewrite and improve caching (#3594)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class SQLiteCache implements CacheInterface
|
||||
{
|
||||
private \SQLite3 $db;
|
||||
private string $scope;
|
||||
private string $key;
|
||||
private array $config;
|
||||
|
||||
public function __construct(array $config)
|
||||
@@ -31,85 +31,77 @@ class SQLiteCache implements CacheInterface
|
||||
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
|
||||
}
|
||||
$this->db->busyTimeout($config['timeout']);
|
||||
// https://www.sqlite.org/pragma.html#pragma_journal_mode
|
||||
$this->db->exec('PRAGMA journal_mode = wal');
|
||||
// https://www.sqlite.org/pragma.html#pragma_synchronous
|
||||
$this->db->exec('PRAGMA synchronous = NORMAL');
|
||||
}
|
||||
|
||||
public function loadData(int $timeout = 86400)
|
||||
public function get(string $key, $default = null)
|
||||
{
|
||||
$cacheKey = $this->createCacheKey($key);
|
||||
$stmt = $this->db->prepare('SELECT value, updated FROM storage WHERE key = :key');
|
||||
$stmt->bindValue(':key', $this->getCacheKey());
|
||||
$stmt->bindValue(':key', $cacheKey);
|
||||
$result = $stmt->execute();
|
||||
if (!$result) {
|
||||
return null;
|
||||
return $default;
|
||||
}
|
||||
$row = $result->fetchArray(\SQLITE3_ASSOC);
|
||||
if ($row === false) {
|
||||
return null;
|
||||
return $default;
|
||||
}
|
||||
$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;
|
||||
$expiration = $row['updated'];
|
||||
if ($expiration === 0 || $expiration > time()) {
|
||||
$blob = $row['value'];
|
||||
$value = unserialize($blob);
|
||||
if ($value === false) {
|
||||
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
|
||||
// delete?
|
||||
return $default;
|
||||
}
|
||||
return $data;
|
||||
return $value;
|
||||
}
|
||||
// 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;
|
||||
// delete?
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function saveData($data): void
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
$blob = serialize($data);
|
||||
|
||||
$cacheKey = $this->createCacheKey($key);
|
||||
$blob = serialize($value);
|
||||
$expiration = $ttl === null ? 0 : time() + $ttl;
|
||||
$stmt = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
|
||||
$stmt->bindValue(':key', $this->getCacheKey());
|
||||
$stmt->bindValue(':key', $cacheKey);
|
||||
$stmt->bindValue(':value', $blob, \SQLITE3_BLOB);
|
||||
$stmt->bindValue(':updated', time());
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public function getTime(): ?int
|
||||
{
|
||||
$stmt = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
|
||||
$stmt->bindValue(':key', $this->getCacheKey());
|
||||
$stmt->bindValue(':updated', $expiration);
|
||||
$result = $stmt->execute();
|
||||
if ($result) {
|
||||
$row = $result->fetchArray(\SQLITE3_ASSOC);
|
||||
if ($row !== false) {
|
||||
return $row['updated'];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
// Unclear whether we should $result->finalize(); here?
|
||||
}
|
||||
|
||||
public function purgeCache(int $timeout = 86400): void
|
||||
public function delete(string $key): void
|
||||
{
|
||||
$key = $this->createCacheKey($key);
|
||||
$stmt = $this->db->prepare('DELETE FROM storage WHERE key = :key');
|
||||
$stmt->bindValue(':key', $key);
|
||||
$result = $stmt->execute();
|
||||
}
|
||||
|
||||
public function prune(): void
|
||||
{
|
||||
if (!$this->config['enable_purge']) {
|
||||
return;
|
||||
}
|
||||
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
|
||||
$stmt->bindValue(':expired', time() - $timeout);
|
||||
$stmt->execute();
|
||||
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated <= :now');
|
||||
$stmt->bindValue(':now', time());
|
||||
$result = $stmt->execute();
|
||||
}
|
||||
|
||||
public function setScope(string $scope): void
|
||||
public function clear(): void
|
||||
{
|
||||
$this->scope = $scope;
|
||||
$this->db->query('DELETE FROM storage');
|
||||
}
|
||||
|
||||
public function setKey(array $key): void
|
||||
private function createCacheKey($key)
|
||||
{
|
||||
$this->key = json_encode($key);
|
||||
}
|
||||
|
||||
private function getCacheKey()
|
||||
{
|
||||
return hash('sha1', $this->scope . $this->key, true);
|
||||
return hash('sha1', $key, true);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user