mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-04 23:57:29 +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:
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user