Here's the code I am using:
<?php
$interval = 5 * 60;
$filename = "cache/".basename( rtrim( $_SERVER["REQUEST_URI"], '/' ) ).".cache";
if ( file_exists( $filename ) && (time() - $interval) < filemtime( $filename ) ) {
readfile( $filename );
exit();
}
ob_start();
include 'dynamicpage.php';
?>
<?php
// More page generation code goes here
$buff = ob_get_contents(); // Retrive the content from the buffer
// Write the content of the buffer to the cache file
$file = fopen( $filename, "w" );
fwrite( $file, $buff );
fclose( $file );
ob_end_flush(); // Display the generated page.
?>
Currently, if the cached page is over 5 minutes old, this script would generate a new cache file to replace the old one. Is there any way I can have the old cache display first and have the new cached page be generated in the background? My host is weak sauce so it takes forever to wait for the page to complete loading.