2012-09-10 52 views
0

我正在尋找刷新後臺供稿緩存的方法。在後臺續訂WordPress供稿緩存

爲了演示我面臨的問題,下面的代碼會有所幫助。當頁面被訪問和加載時,它會每隔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; 
} 

所以我不知道我怎麼能修改此使其默默地更新在後臺緩存當它擊中超時。我的意思是它顯示頁面通常與保存的緩存,雖然超時超過;另一方面,它在訪問後在後臺開始構建新的緩存。通過這種方式,訪問者從不會看到頁面變慢。

可能嗎?

回答

1

好的,這個工程。

<?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分鐘,有時需要4分鐘,儘管我一直按下瀏覽器的重新加載按鈕超過了這些分鐘。

根據食品法典委員會,http://codex.wordpress.org/Function_Reference/wp_schedule_single_event

注意,10分鐘 同名的事件內安排活動時會被忽略,除非你通過爲的$ args唯一值 每個調度事件。

但是日誌文件告訴函數在2分鐘左右的時間間隔內被調用。所以它沒有意義。