This is how I usually do it, however I do not know your URL design and your directory / file layout.
I do this with .htaccess and mod_rewrite & shy; Docs .
The web server checks to see if the cached HTML file exists, and if so, it is delivered. You can also check the age.
If it is too old or it does not exist, your PHP script (s?) Is launched. At the beginning of your script, you run the & shy; output buffer Docs . At the end of your script, you get the output buffer and put the contents in a cache file, and then you output it.
The advantage of this solution is that apache will provide static files if they exist, and there is no need to invoke the PHP process. If you do all this in PHP itself, you will not have this benefit.
I would even take one more step and run a cron job that deletes old cache files instead of checking the time inside .htaccess . To do this, you can make rewriting less complicated to prefer the .php.cached file instead of the .php file.
source share