« Top 5 most dangerous jobs | Main | start programming with PHP »
How to Cache results from a php script
By maurizio | May 13, 2007
If you are new here, you may want to subscribe to my RSS feed. Feel free to leave comments and questions too.Thanks for visiting!
Remember that I said that I will give the whole code of my Alexa ranking script only when I’ll add a cache to it? Well, today I wrote some lines of code to generate a cache file in order to speed up the script and avoid to hammer Alexa’s sites.
Those lines of code can be used on any kind of script. You can use them somewhere else if you wish.
Here is the code:
<?php
$cachefile = "cache.txt";
$expirationTime = 60*60*24*7; //a week
if (file_exists($cachefile) && filemtime($cachefile) > time() - $expirationTime) {
include($cachefile);
echo "from cache";
exit;
}
ob_start();
//your code here...
echo "hi";
//end of your code
$contents = ob_get_contents();
ob_end_clean();
$handle = fopenXXX("$cachefile", "w"); //remove XXX
fwriteXXX($handle, $contents); //remove XXX
fcloseXXX($handle); //remove XXX
include($cachefile);
?>
Note: You must remove XXX from the code in order to make it working.I have to do that because my Wordpress blog is using a secure module (to be correct it’s Apache)
The core of the code is in 3 lines. The first line is the first if statement. That if will check if a cache file exists AND if it’s older than a week. ( check on php.net for the meaning of filemtime ). If the file is too old, it will include it (that should be enough for most of the script, otherwise you could write a function that reads the cache and generate your result).
The second and third important lines are ob_start() and ob_get_contents() ( and ob_end_clean() ). Ob_start blocks all your outputs and redirect them on a buffer. You can use it as a joke if you wish :-). Usually a printor a echo are used to print on the screen something. If you have an ob_start() somewhere before the print/echo lines, those results are not print on the screen..that thing could drive crazy any programmer :-). To get that buffer you use ob_get_contents. The rest of the code just write that content on a file and then include again that file (I could have just print out that buffer..)
It is very important to understand that a cache is extremely useful for speeding up websites.Look for example at some blogs with a lot of scripts that show users communities, information from other sites etc..they are slower to load just because they have to create those information every time thus slowing down the site. If you can cache those data, you will speed up your site. Of course you have to know what are you doing. For example it doesn’t make sense to cache My Visitors list or something else like MyBlogLog because it needs to be updated on every new user on the site, but if you want to show your Alexa rank or feedburner status, it’s perfectly fine (and recommended!) to cache it.
Topics: Content Creation, Programming |
Read other related posts:


March 5th, 2008 at 2:59 pm
Thanks for such a nice script