【问题标题】:Renew WordPress Feed Cache in the Background在后台更新 WordPress Feed 缓存
【发布时间】:2012-09-10 15:31:24
【问题描述】:

我正在寻找一种在后台刷新提要缓存的方法。

为了演示我面临的问题,下面的代码会有所帮助。当页面被访问和加载时,它每 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;
}   

所以我想知道如何修改它,以便它在超时时默默地在后台更新缓存。我的意思是尽管超时超过了,但它会正常显示页面并保存缓存;另一方面,它在访问后开始在后台建立一个新的缓存。这样访问者就不会看到页面变慢了。

有可能吗?

【问题讨论】:

    标签: caching background-process wordpress simplepie


    【解决方案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 分钟,尽管我一直按浏览器的重新加载按钮超过这些分钟。

    根据 Codex,http://codex.wordpress.org/Function_Reference/wp_schedule_single_event

    请注意,在活动开始后 10 分钟内安排活动 相同的名称将被忽略,除非您将 $args 的唯一值传递给 每个预定的事件。

    但是日志文件告诉函数在 2 分钟左右的时间间隔内被调用。所以这没有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-23
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2015-01-20
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多