background-process - 在后台,缓存更新WordPress提要缓存

  显示原文与译文双语对照的内容
130 0

我正在寻找一个方法来刷新背景中的种子缓存。

为了演示我所面对的问题,下面的代码将帮助你。它在访问和加载页面时每 30秒更新一次缓存。因为它有很多url需要同时提取,因这里缓存需要重新生成时变得非常慢。

$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;
} 

所以我想知道如何修改这一点,以便它在点击时在后台静默地更新缓存。我的意思是,它通常使用保存的缓存显示页面,但超时后,它开始在后台建立一个新的缓存。这样访问者就不会看到页面慢了。

是否可能?

时间:原作者:0个回答

57 2

好的这工作。

<?php
/* Plugin Name: Sample Feed Cache Renew Crawler */
 $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'
 );
 $cache_renew_interval = 30;//every thirty seconds
//admin page
 add_action('admin_menu', 'sample_feed_cache_renew_crawler_menu');
 function sample_feed_cache_renew_crawler_menu() {
 add_options_page(
 'Sample Feed Cache Renew Crawler', 
 'Sample Feed Cache Renew Crawler', 
 'manage_options',
 'sample_feed_cache_renew_crawler', 
 'sample_feed_cache_renew_crawler_admin');
 }
 function sample_feed_cache_renew_crawler_admin() {
 global $urls, $cache_renew_interval;
?>
 <div class="wrap">
 <?php 
 $feed = fetch_feed_with_custom_lifetime($urls, 60*60*24 );//lifetime for 24 hours
 if ( $feed->error() )
 return new WP_Error('simplepie-error', $feed->error()); 
 $feed = fetch_feed($urls);
 $i = 0;
 foreach ($feed->get_items() as $item): 
 if (++$i==20) break;
?>
 <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; 
?>
 </div> 
 <?php
 wp_clear_scheduled_hook( 'sample_feed_cache_renew_crawler_event' );
 add_action('sample_feed_cache_renew_crawler_event','sample_feed_cache_renew_crawler_function');
 wp_schedule_single_event(time() + $cache_renew_interval, 'sample_feed_cache_renew_crawler_event');
}
//wp_clear_scheduled_hook( 'sample_feed_cache_renew_crawler_event' );
require_once (ABSPATH. WPINC. '/class-feed.php');
function fetch_feed_with_custom_lifetime($url, $lifetime) {
 $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', $lifetime, $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;
} 
add_action('sample_feed_cache_renew_crawler_event','sample_feed_cache_renew_crawler_function');
function sample_feed_cache_renew_crawler_function() {
 $file = __DIR__. '/log.txt';
 $current = date('l jS of F Y h:i:s A').": cache cleared". PHP_EOL;
 file_put_contents($file, $current, FILE_APPEND);
 global $urls, $cache_renew_interval;
 fetch_feed_with_custom_lifetime($urls, 0);//renew the cache right away
 wp_schedule_single_event(time() + $cache_renew_interval, 'sample_feed_cache_renew_crawler_event');
}

我不清楚,即使我设置了间隔 30秒,它并不总是调用函数,sample_feed_cache_renew_crawler_function()在正确的时间,日志文件告诉我,有时需要 2分钟时间,但是我一直按下浏览器按钮。

根据抄本 http://codex.wordpress.org/Function_Reference/wp_schedule_single_event

注意,如果将事件调度为相同 NAME的10分钟,则将忽略该事件,除非将 $args的唯一值传递给每个。

但是日志文件通知函数是在 2分钟或者更长时间内调用的。所以没有意义。

原作者:
...