Use your left/right keys to browse tutorials
Caching Dynamic PHP pages easily

Caching Dynamic PHP pages easily

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on February 17, 2009

I’ve been looking for a solution to cache heavy pages and just serve a static html version for a little while now, and I’ve found a solution in output buffering.

Things to think about

It’s not a good idea to go away and cache your entire site, you need to think about which pages receive high traffic, and which pages make a number of database requests. Static HTML pages aren’t going to see a benefit from caching and may in fact be served slower due to PHP invoking the request to the cached version. as an example I’m using caching on dotdashcreate.com homepage as there are a number of database requests that could easily be cached, the cached version of the page is stored here.

If you run a big site or blog I would certainly recommend caching your homepage as this will usually be your visitors first point of contact thus generating more traffic. Its probably not a good idea to cache individual posts that allow comments etc, unless you are willing to write a script to re-cache the page.

You need to allow write access to the cache directory, in the code example this is /cache/. There’s quite a bit going on in the script, the first two lines set the path to the cached directory and the time frame to refresh the cache, we then do a check to see if the cached file is older than the cache time, if it is then it refreshes the cached version (which is the block of code at the bottom), if not it just serves the cached version.

The Code:


$cachefile = 'cache.html';
$cachetime = 4 * 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    include($cachefile);
    echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
    exit;
}
ob_start(); // Start the output buffer

/* Heres where you put your page content */

// Cache the contents to a file
$cached = fopen($cacheFile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser

demo



More tutorials from Papermashup
Comments
63 discussions around Caching Dynamic PHP pages easily
Older Comments
  1. Ken Le says:

    Try this one, go to Example, they have an example for caching whole website.
    This class http://www.phpfastcache.com is supported Files, APC, Memcached and more.. It’s simple for newbie

  2. Thank you, however im having difficulties caching same page with 3 different changing variables. anyway thanks for help

  3. I’m excited to discover this web site. I want to to thank you for ones time due to this fantastic read!! I definitely savored every bit of it and I have you saved as a favorite to see new things in your website.

  4. You made several good tips there. I did a search on the subject and discovered
    a lot of people will agree with your blog. Wonderful Job,
    Chow!

  5. I have read so many articles or reviews concerning the blogger lovers except this article is in fact a
    good paragraph, keep it up.

  6. Fatriff says:

    Recently I started a new project where the best solution would be to generate static pages using this method because everything on the site is dynamic so having constantly changing text and things would hurt the site in seo.. So i’m glad I found this again!

  7. jackie says:

    I love you so

  8. Pingback: Useful Components of PHP , Techniques and Tutorials | Rapidesigners

  9. Pingback: Highly Useful Tools and Techniques for PHP Developers | Techno Tab

Older Comments




Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.