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
  • http://www.phpfastcache.com Ken Le

    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

  • http://downloadfiles.info/grand-theft-auto-5-pc-beta-download/ Grand theft auto 5 pc download

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

  • http://freereviewed.com/kou-tea-review/ is green tea good for weight loss

    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.

  • http://autochargers.page.tl/General-Trends-Pertaining-To-Car-Battery-Solar-Charger.htm automotive battery charger

    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!

  • http://newrepublicoftexas.com/nrt/groups/ask-yourself-how-michael-kors-was-working-in-a-lot-of-people-fashion-bloggers/ Michael Kors Handbags Outlet

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

  • http://www.floristsnear.co.uk Fatriff

    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!

  • jackie

    I love you so

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

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