PHP和AJAX:缓存RSS数据

PHP和AJAX:缓存RSS数据,php,xml,caching,rss,Php,Xml,Caching,Rss,考虑以下AJAX调用,以便在单击事件时加载RSS新闻数据: $(function() { $(document).ready(function() { $('.msg_head').eq(0).click(function(){ $('.msg_body').eq(0).load('printSideNews.php'); $('.loadMessage').

考虑以下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);

我需要缓存
$itemD
$xmlD
对象-不确定是哪一个。这应该被缓存1小时,然后再次从url获取。非常感谢您对代码缓存的任何帮助。

看看这个函数:

    <?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);

您的问题是什么?到目前为止,您试图解决什么问题?这不是“租一个编码器”而是“付一个0”的帮助,因此,除非你澄清你尝试了什么以及失败的地方,否则你是在错误的地方。你有任何类型的用户会话状态吗?我不会构建用户功能。该网站将只有访问者。我们的想法是使用一些文件夹来缓存不使用会话的内容。目前我正在使用localhost和XAMPP服务器,因此URL是不必要的?将您的代码更新为一个示例,您的newsFeed.php可能看起来像什么,1小时缓存您的feed非常有用,现在我将尝试实现它解析在Web服务器上应该不是一个大问题,因此,更改提要应该是好的。更新了我的代码以缓存该项,它未经测试,但应该可以工作
    <?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);