I'm looking for a way to refresh feed caches in the background.
To demonstrate the issue I'm facing with, the below code would help. It renews the cache in every 30 seconds when the page is accessed and loaded. Since it has lots of urls to fetch at once, it gets really slow when the cache needs to be rebuild.
$urls = array(
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=w&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=n&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=b&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=el&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=tc&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=ir&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=s&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=snc&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=m&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=e&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=topic:bagram&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=topic:syria&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=topic:baghdad&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=topic:bernard_arnault&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=topic:senkaku_islands&output=rss',
'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=topic:alps&output=rss'
);
$feed = fetch_feed_modified($urls);
foreach ($feed->get_items() as $item):
?>
<div class="item">
<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
<p><?php echo $item->get_description(); ?></p>
<p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
</div>
<?php endforeach;
function fetch_feed_modified($url) {
require_once (ABSPATH . WPINC . '/class-feed.php');
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->set_cache_class('WP_Feed_Cache');
$feed->set_file_class('WP_SimplePie_File');
$feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 30, $url)); // set the cacne timeout to 30 seconds
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
$feed->handle_content_type();
if ( $feed->error() )
return new WP_Error('simplepie-error', $feed->error());
return $feed;
}
So I'm wondering how I can modify this so that it silently renews the cache in the background when it hits the timeout. I mean it shows the page normally with the saved cache although the timeout exceeds; on the other hand, it starts building a new cache in the background after the access. This way the visitor never sees the page being slow.
Is it possible?
Okay, this works.
One thing that is not clear to me is that even though I set the interval to 30 seconds, it's not always calling the function,
sample_feed_cache_renew_crawler_function()
in the right time. The log file tells that sometimes it takes 2 minutes and sometimes 4 minutes although I kept pressing the reload button of the browser for more than those minutes.According to Codex, http://codex.wordpress.org/Function_Reference/wp_schedule_single_event,
But the log file tells function was called in an interval of 2 minutes or so. So it doesn't make sense.