update_rss_feed: use DiskCache to store feed data

This commit is contained in:
Andrew Dolgov 2022-11-23 22:09:04 +03:00
parent 10a1dd35e3
commit 9732d8fc9f
No known key found for this signature in database
GPG Key ID: 1A56B4FA25D4AF2A
4 changed files with 23 additions and 7 deletions

View File

@ -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
*

View File

@ -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));
}

View File

@ -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);
}

View File

@ -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);
}
}
}