Easy Server Side Caching in PHP

Tagged:  

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.

[...] Easy Server Side Caching in PHP By David Hurth 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 … Ajaxonomy - The Study of Ajax… - http://www.ajaxonomy.com [...]

Great tutorial !! I need to do this sometime, if I ever get a site w/ enough traffic that is.
You have a good seo tutorial to go w/ it lol. :)

great post. i liked it... Like the Paranaque Scandal...

There are few things I'd like to point out.

First, it's better to use readfile() than include() to output the cached page. Using readfile() is better, because the include() parses the file as PHP, and if your output has php tags or, say, xml tags with short tags enabled, including may yield unexpected results. Plus, since the page is simply read to the output buffer, it's should be more efficient.

Secondly, if you are caching an entire page and not just some parts of the page, you should considering implementing support for conditional get as well. This means returning a 304 header instead of the page, if it has not been modified since browser last viewed the page. This can significantly decrease the webserver load, if same pages are requested by the same user over and over again. For more information just search for "conditional get" on google. I've also written a tutorial about handling it with PHP at:

http://rithiur.anthd.com/tutorials/conditionalget.php

Rithiur,

Thanks for your comments. The example code was from a cached page that produced JSON, so it had no issues, but XML would have a problem, so thanks for the correction.

As for your second comment, that would be helpful in quite a few applications. You would use it the way in the post when you didn't want to accept parameters, like in a public feed that is the same for all users.

Thanks again for your comments.

[...] 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. (full story) [...]

I use always caching for the site is that is not real time. Because caching is very improve performance and badwith issuse.

nakliyat
saç ekimi

hello
say if you have a fourm
you cant do this cuz your pages will updataed every scend

thank you
------------------
العاب
برامج العاب
مفضلتى

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

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

I've also written a tutorial about handling it with PHP at:
lazer epilasyon

Great tutorial !! I need to do this sometime, if I ever get a site w/ enough traffic that is.

[...] aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

[...] bilgi sahibi olmak isteyenlerin i?ine yarayacak pek çok makaleyi içeriyor. Bunun yan?s?ra PHP’de sunucu tabanl? caching gibi di?er AJAX teknolojilerine ait bilgiler ve web üzerindeki di?er AJAX rehberlerinin [...]

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

[...] an aggregate of posts that either share tricks and techniques related to Ajax technologies (such as server-side caching in PHP) or reviews of tutorials elsewhere on the [...]

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <div> <blockquote> <object> <embed> <img> <param>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.