How to put an RSS feed in cache

We can save resources by placing the converted HTML form of an RSS feed to a file rather than parsing the XML and convert it to HTML on each display.

However, this cache file must be updated automatically and regularly, eg every hour, as in the example code below.

Replace the usual code to call RSSLIB in the web page that display the feed (the url is an example):

$url = "";
echo RSS_Display($url, 15, false, true);

by the following code:

$cachename = "rss-cache-tmp.php"; 
$url = ""; 
  $now = date("G");
  $time = date("G", filemtime($cachename));
  if($time == $now)
$cache = RSS_Display($url, 15, false, true);
file_put_contents($cachename, $cache); echo $cache; ?>

For a different update frequency, change the date() format and the comparison test.

For example, for an update every ten minutes, the format will be:

  $time = date("i", filemtime($cachename));

and the comparison test will be:

  if(intval($time) / 10  == intval($now) / 10)

See the demo: Caching RSS.

Download the archive from the article: RSS Reader.