diff options
author | 2023-07-19 05:05:49 +0200 | |
---|---|---|
committer | 2023-07-19 05:05:49 +0200 | |
commit | 6254b8593e2f7636db65db23c1228482e38be44f (patch) | |
tree | f30cb00666c8231e741b1151aba84eb338702e98 /caches/SQLiteCache.php | |
parent | 087e790ec10d287f944e3abeb5ab3bda9a1a045a (diff) | |
download | rss-bridge-6254b8593e2f7636db65db23c1228482e38be44f.tar.gz rss-bridge-6254b8593e2f7636db65db23c1228482e38be44f.tar.zst rss-bridge-6254b8593e2f7636db65db23c1228482e38be44f.zip |
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
Diffstat (limited to 'caches/SQLiteCache.php')
-rw-r--r-- | caches/SQLiteCache.php | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/caches/SQLiteCache.php b/caches/SQLiteCache.php index f9258a88..d7ab1374 100644 --- a/caches/SQLiteCache.php +++ b/caches/SQLiteCache.php @@ -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(); } |