I've used this code for caching a huge page. My code queries another website so I need to use cache because it would take about 5 minutes to load.
<?php
$cachefile = "cache/".$reqfilename.".html";
$cachetime = 5 * 60 * 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->n";
exit;
}
ob_start(); // start the output buffer
?>
// .. Your usual PHP script and HTML here ...
<?php
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
?>
The problem is that from time to time a user has to wait for the page to cache. How can I solve this? Is any possibility to put a system job or something to load that page from 5 to 5 hours?
Thank you in advance.