summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2022-11-23 22:09:04 +0300
committerAndrew Dolgov <[email protected]>2022-11-23 22:09:04 +0300
commit9732d8fc9ff976bea480b663012c6c6ec6dc9f01 (patch)
tree49932ff52a2fe97655666ba4e06b5988511286f7 /classes
parent10a1dd35e3188674db61cce3a2cc64fd5fa1f596 (diff)
update_rss_feed: use DiskCache to store feed data
Diffstat (limited to 'classes')
-rw-r--r--classes/cache/adapter.php5
-rw-r--r--classes/cache/local.php4
-rw-r--r--classes/diskcache.php6
-rwxr-xr-xclasses/rssutils.php15
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);
}
}
}