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):
<?php
require_once("rsslib.php");
$url = "https://www.scriptol.com/rss.xml";
echo RSS_Display($url, 15, false, true);
?>
by the following code:
<?php
$cachename = "rss-cache-tmp.php";
$url = "https://www.scriptol.com/rss.xml";
if(file_exists($cachename))
{
$now = date("G");
$time = date("G", filemtime($cachename));
if($time == $now)
{
include($cachename);
exit();
}
}
include("rsslib.php");
$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.