cache simplepie object instead of xml feed data

This commit is contained in:
Andrew Dolgov 2013-03-29 11:50:58 +04:00
parent d5974daa33
commit 4f9cbdff1d
1 changed files with 60 additions and 47 deletions

View File

@ -149,7 +149,6 @@
if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]); if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
update_rss_feed($link, $line["id"], true); update_rss_feed($link, $line["id"], true);
sleep(1); // prevent flood (FIXME make this an option?)
} }
require_once "digest.php"; require_once "digest.php";
@ -205,7 +204,7 @@
$date_feed_processed = date('Y-m-d H:i'); $date_feed_processed = date('Y-m-d H:i');
$cache_filename = CACHE_DIR . "/simplepie/" . sha1($fetch_url) . ".xml"; $cache_filename = CACHE_DIR . "/simplepie/" . sha1($fetch_url) . ".feed";
// Ignore cache if new feed or manual update. // Ignore cache if new feed or manual update.
$cache_age = ($no_cache || is_null($last_updated) || $last_updated == '1970-01-01 00:00:00') ? $cache_age = ($no_cache || is_null($last_updated) || $last_updated == '1970-01-01 00:00:00') ?
@ -218,6 +217,9 @@
$cached_feed_data_hash = false; $cached_feed_data_hash = false;
$rss = false;
$rss_hash = false;
if (file_exists($cache_filename) && if (file_exists($cache_filename) &&
is_readable($cache_filename) && is_readable($cache_filename) &&
!$auth_login && !$auth_pass && !$auth_login && !$auth_pass &&
@ -227,9 +229,15 @@
_debug("update_rss_feed: using local cache."); _debug("update_rss_feed: using local cache.");
} }
$feed_data = file_get_contents($cache_filename); @$rss_data = file_get_contents($cache_filename);
$cached_feed_data_hash = sha1($feed_data);
if ($rss_data) {
$rss_hash = sha1($rss_data);
@$rss = unserialize($rss_data);
} }
}
if (!$rss) {
if (!$feed_data) { if (!$feed_data) {
if ($debug_enabled) { if ($debug_enabled) {
@ -238,6 +246,11 @@
$feed_data = fetch_file_contents($fetch_url, false, $feed_data = fetch_file_contents($fetch_url, false,
$auth_login, $auth_pass, false, $no_cache ? 15 : 45); $auth_login, $auth_pass, false, $no_cache ? 15 : 45);
if ($debug_enabled) {
_debug("update_rss_feed: fetch done.");
}
} }
if (!$feed_data) { if (!$feed_data) {
@ -255,17 +268,6 @@
return; return;
} }
// cache data for later
if (!$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) {
if (sha1($feed_data) != $cached_feed_data_hash) {
if ($debug_enabled) {
_debug("update_rss_feed: saving $cache_filename");
}
@file_put_contents($cache_filename, $feed_data);
}
} }
$pluginhost = new PluginHost($link); $pluginhost = new PluginHost($link);
@ -280,10 +282,7 @@
$feed_data = $plugin->hook_feed_fetched($feed_data); $feed_data = $plugin->hook_feed_fetched($feed_data);
} }
if ($debug_enabled) { if (!$rss) {
_debug("update_rss_feed: fetch done, parsing...");
}
$rss = new SimplePie(); $rss = new SimplePie();
$rss->set_sanitize_class("SanitizeDummy"); $rss->set_sanitize_class("SanitizeDummy");
// simplepie ignores the above and creates default sanitizer anyway, // simplepie ignores the above and creates default sanitizer anyway,
@ -294,6 +293,7 @@
$rss->enable_cache(false); $rss->enable_cache(false);
@$rss->init(); @$rss->init();
}
// print_r($rss); // print_r($rss);
@ -301,6 +301,19 @@
if (!$rss->error()) { if (!$rss->error()) {
// cache data for later
if (!$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) {
$rss_data = serialize($rss);
$new_rss_hash = sha1($rss_data);
if ($new_rss_hash != $rss_hash) {
if ($debug_enabled) {
_debug("update_rss_feed: saving $cache_filename");
}
@file_put_contents($cache_filename, serialize($rss));
}
}
// We use local pluginhost here because we need to load different per-user feed plugins // We use local pluginhost here because we need to load different per-user feed plugins
$pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss); $pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss);