When developing dynamic web applications there may be many times that data is not updated for a certain amount of time. In PHP it is fairly simple to have a page cached, so that there is not much load on the server.
To create this caching you would put some code like the following on the top of your PHP page.
$cachefile = 'caching_folder/cachedpage.html'; $cachetime = 30; // Serve from the cache if it is younger than $cachetime if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { include($cachefile); exit; } ob_start(); // Start the output buffer
The things that you would change in the above code is the setting of the $cachfile variable to the name of the static file that the cache will be saving on the server (you will need to set permissions on the folder where you want to save the cached file). You will also want to change the $cachetime variable to the number of seconds between each caching.
Now you would put all of your standard PHP code to create the page after the above code. Once you have all of your standard PHP code you would put the below code to actually save the cache file.
// Cache the output to a file $fp = fopen($cachefile, 'w'); fwrite($fp, ob_get_contents()); fclose($fp); ob_end_flush(); // Send the output to the browser
This script is very handy and a version of it is used in the del.icio.us Spy application to avoid too many calls to the del.icio.us servers.
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket
Recent comments
4 weeks 19 hours ago
5 weeks 2 days ago
6 weeks 3 days ago
6 weeks 4 days ago
6 weeks 6 days ago
7 weeks 1 day ago
7 weeks 2 days ago
9 weeks 4 days ago
10 weeks 2 hours ago
10 weeks 4 hours ago