Permalink
Browse files

Add basic caching support

  • Loading branch information...
1 parent ea0b37f commit f7e74c72f69319051796b97f75c4e8ed58b0a781 @gokmen committed Mar 2, 2012
Showing with 26 additions and 0 deletions.
  1. +26 −0 index.php
View
@@ -12,6 +12,20 @@
if (file_exists('config.ini') and file_exists('pages.ini')) {
# Main Nuf (~Fun) Content Management Class
include("nuf/nuf.class.php");
+ $cachefile = "cache/".$_GET["page"].".html";
+
+ // Serve from the cache if it is the same age or younger than the last
+ // modification time of the included file (includes/$reqfilename)
+
+ $cachetime = 5 * 60;
+ if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
+ include($cachefile);
+ echo "<!-- Cached at ".date('H:i', filemtime($cachefile))." -->\n";
+ exit;
+ }
+
+ // start the output buffer
+ ob_start();
# Create a new instance
$PCM = new Nuf();
@@ -27,4 +41,16 @@
echo "Please look at README.md file.";
}
+ // 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();
+
?>

0 comments on commit f7e74c7

Please sign in to comment.