up vote 0 down vote favorite
share [fb]

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.

link|improve this question

What's your question and what have you tried so far to solve your problem? This isn't rent-a-coder-but-pay-0-for-help, so unless you clarify what you have tried and where you've failed - you're at the wrong place. – N.B. Oct 12 at 13:58
Do you have any kind of session state for the user ? – Interstellar_Coder Oct 12 at 14:04
I won't build user functionality. The site will have only visitors. The idea is to have some folder for caching not to use Sessions. – George Oct 12 at 14:08
feedback

1 Answer

up vote 1 down vote accepted

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);
link|improve this answer
At this time I'm using localhost with XAMPP server so URL's aren't necessary? – George Oct 12 at 14:14
1  
updated your code to an example, waht your newsFeed.php could look like, with 1 hour caching of your feed – Frederick Behrends Oct 12 at 14:19
Really helpful, now I'll try to implement it – George Oct 12 at 14:25
1  
the parsing should not be a big problem on a webserver, so chaching the feed should be good. – Frederick Behrends Oct 12 at 14:41
1  
updated my code to cache the item, its untested but should be working – Frederick Behrends Oct 12 at 14:52
show 2 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.