PHP 和 AJAX:缓存 RSS 数据

发布于 2024-12-09 05:08:31 字数 1153 浏览 0 评论 0原文

考虑以下 AJAX 调用,以在单击事件上加载 RSS 新闻数据:

 $(function() {
            $(document).ready(function() {
                $('.msg_head').eq(0).click(function(){
                    $('.msg_body').eq(0).load('printSideNews.php');
                    $('.loadMessage').eq(2).hide();
                });
    });
});

printSideNews.php 如下所示:

include ('newsFeed.php');
  function printNewsMenu($itemD){
    for ($i = 0; $i < 4; $i++) {
            if($itemD==null)
            echo "An error has occured, please try again later";
            $article_title = $itemD[$i]->title;
            $article_link = $itemD[$i]->link;
            echo "<a href='".$article_link."' title='".$article_title."' target='_blank'>". $article_title. " </a></br>". PHP_EOL;
}
printNewsMenu($itemD);

包含的 newsFeed.php 文件类似于:

$urlD = "someurl";
    $xmlD = simplexml_load_file($urlD);
    $itemD = $xmlD->channel->item;

我需要缓存 < code>$itemD 或 $xmlD 对象 - 不确定是哪一个。该内容应缓存 1 小时,然后再次从 url 获取。任何有关代码缓存这一混乱的帮助将不胜感激。

Consider the following AJAX call to load RSS news data on click event:

 $(function() {
            $(document).ready(function() {
                $('.msg_head').eq(0).click(function(){
                    $('.msg_body').eq(0).load('printSideNews.php');
                    $('.loadMessage').eq(2).hide();
                });
    });
});

printSideNews.php looks like this:

include ('newsFeed.php');
  function printNewsMenu($itemD){
    for ($i = 0; $i < 4; $i++) {
            if($itemD==null)
            echo "An error has occured, please try again later";
            $article_title = $itemD[$i]->title;
            $article_link = $itemD[$i]->link;
            echo "<a href='".$article_link."' title='".$article_title."' target='_blank'>". $article_title. " </a></br>". PHP_EOL;
}
printNewsMenu($itemD);

The included newsFeed.php file lloks like:

$urlD = "someurl";
    $xmlD = simplexml_load_file($urlD);
    $itemD = $xmlD->channel->item;

I need to cache the $itemD or the $xmlD object - not sure which one. This should be cached for 1 hour then taken again from the url. Any help with code caching this mess will be greatly appreciated.

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

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

发布评论

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

评论(1

执手闯天涯 2024-12-16 05:08:31

看一下这个函数:

    <?php      
    function cacheObject($url,$name,$age = 86400)
      { 
        // directory in which to store cached files
        $cacheDir = "cache/";
        // cache filename constructed from MD5 hash of URL
        $filename = $cacheDir.$name;
        // default to fetch the file
        $cache = true;
        // but if the file exists, don't fetch if it is recent enough
        if (file_exists($filename))
        {
          $cache = (filemtime($filename) < (time()-$age));
        }
        // fetch the file if required
        if ($cache)
        {
          $xmlD = simplexml_load_file($url);
          $itemD = $xmlD->channel->item;
          file_put_contents($filename,serialize($itemD));
          // update timestamp to now
          touch($filename);
        }
        // return the cache filename
        return unserialize(file_get_contents($filename));
      }     


$urlD = "someurl";

$itemD = cacheObject($urlD,'cacheobject',3600);

take a look at this function:

    <?php      
    function cacheObject($url,$name,$age = 86400)
      { 
        // directory in which to store cached files
        $cacheDir = "cache/";
        // cache filename constructed from MD5 hash of URL
        $filename = $cacheDir.$name;
        // default to fetch the file
        $cache = true;
        // but if the file exists, don't fetch if it is recent enough
        if (file_exists($filename))
        {
          $cache = (filemtime($filename) < (time()-$age));
        }
        // fetch the file if required
        if ($cache)
        {
          $xmlD = simplexml_load_file($url);
          $itemD = $xmlD->channel->item;
          file_put_contents($filename,serialize($itemD));
          // update timestamp to now
          touch($filename);
        }
        // return the cache filename
        return unserialize(file_get_contents($filename));
      }     


$urlD = "someurl";

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