PHP:缓存 RSS 数据以避免每次加载页面时都请求它

发布于 2024-08-24 18:11:10 字数 495 浏览 3 评论 0原文

我正在解析来自此 RSS 源的数据: http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=SAM|AR|AR005|MONTE%20MAIZ,那里的数据每小时更新一次。

为了解析它,我在类初始化时加载 RSS 并将其转换为可用的对象。

这样做的问题是,每次页面加载时,RSS都会被重新解析,HTTP请求会延迟页面加载。

我正在考虑使用一个cronjob来每小时解析RSS,然后将数据保存在JSON结构中。但我真的不想依赖 cron,因为并非所有共享托管服务器都提供它(尽管它们应该提供)。

关于如何执行此操作还有其他建议吗?

I'm parsing data from this RSS feed: http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=SAM|AR|AR005|MONTE%20MAIZ, The data there updates once every hour.

To parse it, I load the RSS at the initialization of my class and convert it into an usable object.

The problem with this is that every time the page loads, the RSS is parsed again, and the HTTP request delays the page loading.

I was thinking of a cronjob to parse the RSS hourly, and then save the data in a JSON structure. But I'd really like to not depend on the cron, as not all shared hosting servers provide it (although they should).

Any other suggestions on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2024-08-31 18:11:10

您可以使用 Zend_Cache。服务器将首次加载提要,但稍后的调用将利用缓存。

实际上,我已经实现了您所详细说明的内容:

private function _loadFeedEntries($url)
{
    $cache = $this->_getCacheObj();
    $md5   = md5($url);
    if ($result = $cache->load("feed_{$md5}")) {
        return $result;
    }
    $entries = array();
    try {
        $feed = @Zend_Feed::import($url);
        foreach ($feed as $entry) {
            $entries[] = $entry;
        }
    } catch (Exception $e) {
        // ignore this feed.
    }

    $cache->save($entries, "feed_{$md5}");

    return $entries;
}

private function _getCacheObj()
{
    $frontendOptions = array(
        'lifetime' => self::CACHE_LIFETIME,
        'automatic_serialization' => true
    );

    $backendOptions = array(
        'cache_dir' => self::CACHE_DIR
    );

    return Zend_Cache::factory(
        'Core',
        'File',
        $frontendOptions,
        $backendOptions
    );
}

您仍然可以让 cronjob 调用 _loadFeedEntries(),这将缓存结果。
您甚至可以在系统中设置 cronjob,每隔 CACHE_LIFETIME 秒导航到您的站点,从而“保持”缓存更新。

You can use Zend_Cache. The server will load the feeds first time, but later calls will utilize the cache.

Actually, I've implemented what you have detailed:

private function _loadFeedEntries($url)
{
    $cache = $this->_getCacheObj();
    $md5   = md5($url);
    if ($result = $cache->load("feed_{$md5}")) {
        return $result;
    }
    $entries = array();
    try {
        $feed = @Zend_Feed::import($url);
        foreach ($feed as $entry) {
            $entries[] = $entry;
        }
    } catch (Exception $e) {
        // ignore this feed.
    }

    $cache->save($entries, "feed_{$md5}");

    return $entries;
}

private function _getCacheObj()
{
    $frontendOptions = array(
        'lifetime' => self::CACHE_LIFETIME,
        'automatic_serialization' => true
    );

    $backendOptions = array(
        'cache_dir' => self::CACHE_DIR
    );

    return Zend_Cache::factory(
        'Core',
        'File',
        $frontendOptions,
        $backendOptions
    );
}

You still can make cronjob to call _loadFeedEntries(), which will cache the result.
You can even make cronjob in your system to navigate to your site every CACHE_LIFETIME seconds, thus "keeping" the cache updated.

北恋 2024-08-31 18:11:10

您可以根据页面请求进行(非常)简单的缓存,以避免依赖 cron。

也就是说:

当客户端请求您的页面时,尝试从缓存文件加载数据。如果缓存文件不存在或太旧,请访问服务的 RSS 源进行更新并将其写入缓存文件。

写入缓存,

如果您使用file_put_contents( FILENAME, json_encode( $some_data_struct ));

读取它

您可以使用简单的json_decode( file_get_contents( FILENAME ));

You could do (very) simple caching upon page request, to avoid relying on cron.

That is:

When a client requests your page, try loading the data from a cache file. If the cache file doesn't exist, or is too old, hit the service's RSS feed for an update and write it to a cache file.

If you write to the cache with

file_put_contents( FILENAME, json_encode( $some_data_structure ));

you can read it in with a simple

json_decode( file_get_contents( FILENAME ));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文