在后台续订WordPress Feed缓存 [英] Renew WordPress Feed Cache in the Background

查看:94
本文介绍了在后台续订WordPress Feed缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在后台刷新Feed缓存的方法.

I'm looking for a way to refresh feed caches in the background.

为演示我面临的问题,下面的代码会有所帮助.当访问和加载页面时,它每30秒更新一次缓存.由于它具有大量要同时获取的网址,因此当需要重建缓存时,它的速度确实很慢.

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.

有可能吗?

推荐答案

好的,这可行.

<?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分钟.

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.

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

请注意,在 相同的名称将被忽略,除非您将$ args的唯一值传递给 每个预定的事件.

Note that scheduling an event within 10 minutes of an event of the same name will be ignored, unless you pass unique values for $args to each scheduled event.

但是日志文件告诉函数在2分钟左右的间隔内被调用.因此,这没有任何意义.

But the log file tells function was called in an interval of 2 minutes or so. So it doesn't make sense.

这篇关于在后台续订WordPress Feed缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆