tags:

views:

50

answers:

4
+2  Q: 

PHP RSS caching

Hello,

I was looking for a solution on caching RSS feeds in PHP. I was planning to do the parsing with Magpie RSS parser (http://magpierss.sourceforge.net/). But then how could I go about caching feeds (in case sometimes feed provider won't let me read the same feed, etc.)?

Regards.

+2  A: 
  • You fetch the feed. Save the results to a database or file (serialize()).
  • When it is time to fetch the feed again,
  • Check if the file exists, if not create and update
  • If the file exists, check the the timestamp of the db or file.
  • if it is older than your threshold (say 20 secondsm) then you refetch, otherwise you just return the cached feed.
  • If you can't fetch the feed for whatever reason, you return the cached version up to a timeout period (say 20 minutes)

Voila caching.

Byron Whitlock
You can also do a conditional GET to avoid transferring the feed again if it hasn't changed.
Artefacto
A: 

fetch a list of feeds say, every hour using wget.

Write them into a folder called /cache

Repeat.

This would work fine as long as a) hourly is good enough b) hourly is good enough for all feeds and c) you have access to cron

Gotta wonder why you are fetching and serving the feeds unless you are doing some post-fetch analysis on them though.

Cups
A: 

doesn't magpie have built in caching? Why not use that? I personally use SimplePie. Here is the doc for caching for SimplePie :

http://simplepie.org/wiki/faq/how_does_simplepie_s_caching_http_conditional_get_system_work

Angel S. Moreno
I wonder why I was voted down? :-(
Angel S. Moreno
A: 

@Byron Whitlock Ok, I was doing ~same thing. But I decided not to use MagPie. Instead I use rss2html.php which generates HTML code from RSS i fetch, based on HTML template I provide it with. When I say include("rss2html.php"), it generates HTML. So instead of caching RSS, I am caching this already generated HTML. This is my very simple block of code:

<?php 
            $hashfromURL = hash("md5",$url);
            $cachefile = "cache/rss/".$hashfromURL.".html";

            $cachetime = 5*60; //5 minuta TODO:Pri deployment-u povecati na sat-dva.
            //Serviraj is kesha ako je mladji od $cachetime 
            if(file_exists($cachefile) && (time() - filemtime($cachefile) < $cachetime ))
            {
                include($cachefile);
                echo "RSS ucitan iz kesha!";

            }
            else{//Ucitaj RSS ponovo    

                $XMLfilename = $url;

                //Pocni dump buffera
                ob_start();

                include("rss2html.php");//HTML parsiran sadrzaj RSS-a

                //Otvori kesh fajl za pisanje
                $fp = fopen($cachefile, 'w');

                //Sacuvaj sadrzaj izlaznog buffer-a u fajl
                fwrite($fp, ob_get_contents());

                //zatvori fajl
                fclose($fp);

                //Posalji izlaz na browser
                ob_end_flush(); 
                echo "RSS osvjezen - feed ponovo ucitan!";
            }

    ?>
ZeKoU