From 9732d8fc9ff976bea480b663012c6c6ec6dc9f01 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Nov 2022 22:09:04 +0300 Subject: [PATCH] update_rss_feed: use DiskCache to store feed data --- classes/cache/adapter.php | 5 +++++ classes/cache/local.php | 4 ++++ classes/diskcache.php | 6 +++++- classes/rssutils.php | 15 +++++++++------ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/classes/cache/adapter.php b/classes/cache/adapter.php index da69c5bb1..88dca21b9 100644 --- a/classes/cache/adapter.php +++ b/classes/cache/adapter.php @@ -9,6 +9,11 @@ interface Cache_Adapter { * @return int|false -1 if the file doesn't exist, false if an error occurred, size in bytes otherwise */ public function get_size(string $filename); + + /** + * @return int|false -1 if the file doesn't exist, false if an error occurred, timestamp otherwise + */ + public function get_mtime(string $filename); /** * @param mixed $data * diff --git a/classes/cache/local.php b/classes/cache/local.php index 20d97d9c0..4461ef4ca 100644 --- a/classes/cache/local.php +++ b/classes/cache/local.php @@ -2,6 +2,10 @@ class Cache_Local implements Cache_Adapter { private string $dir; + public function get_mtime(string $filename) { + return filemtime($this->get_full_path($filename)); + } + public function set_dir(string $dir) : void { $this->dir = Config::get(Config::CACHE_DIR) . "/" . basename(clean($dir)); } diff --git a/classes/diskcache.php b/classes/diskcache.php index 4cec77cde..69b48688c 100644 --- a/classes/diskcache.php +++ b/classes/diskcache.php @@ -200,7 +200,7 @@ class DiskCache implements Cache_Adapter { if (implements_interface($p, "Cache_Adapter")) { /** @var Cache_Adapter $p */ - $this->adapter = $p; + $this->adapter = clone $p; // we need separate object instances for separate directories $this->adapter->set_dir($dir); return; } @@ -210,6 +210,10 @@ class DiskCache implements Cache_Adapter { $this->adapter->set_dir($dir); } + public function get_mtime(string $filename) { + return $this->adapter->get_mtime($filename); + } + public function set_dir(string $dir) : void { $this->adapter->set_dir($dir); } diff --git a/classes/rssutils.php b/classes/rssutils.php index fe295417a..2a82a35b0 100755 --- a/classes/rssutils.php +++ b/classes/rssutils.php @@ -338,6 +338,9 @@ class RSSUtils { $pdo = Db::pdo(); + /** @var DiskCache $cache */ + $cache = new DiskCache('feeds'); + if (Config::get(Config::DB_TYPE) == "pgsql") { $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'"; } else { @@ -387,7 +390,7 @@ class RSSUtils { $date_feed_processed = date('Y-m-d H:i'); - $cache_filename = Config::get(Config::CACHE_DIR) . "/feeds/" . sha1($feed_obj->feed_url) . ".xml"; + $cache_filename = sha1($feed_obj->feed_url) . ".xml"; $pluginhost = new PluginHost(); $user_plugins = get_pref(Prefs::_ENABLED_PLUGINS, $feed_obj->owner_uid); @@ -423,13 +426,13 @@ class RSSUtils { // try cache if (!$feed_data && - is_readable($cache_filename) && + $cache->exists($cache_filename) && !$feed_obj->auth_login && !$feed_obj->auth_pass && - filemtime($cache_filename) > time() - 30) { + $cache->get_mtime($cache_filename) > time() - 30) { Debug::log("using local cache: {$cache_filename}.", Debug::LOG_VERBOSE); - $feed_data = file_get_contents($cache_filename); + $feed_data = $cache->get($cache_filename); if ($feed_data) { $rss_hash = sha1($feed_data); @@ -480,9 +483,9 @@ class RSSUtils { if ($feed_data && !$feed_obj->auth_pass && !$feed_obj->auth_login && is_writable(Config::get(Config::CACHE_DIR) . "/feeds")) { $new_rss_hash = sha1($feed_data); - if ($new_rss_hash != $rss_hash) { + if ($new_rss_hash != $rss_hash && $cache->is_writable()) { Debug::log("saving to local cache: $cache_filename", Debug::LOG_VERBOSE); - file_put_contents($cache_filename, $feed_data); + $cache->put($cache_filename, $feed_data); } } }