PHP
  Home arrow PHP arrow Page 4 - Output Caching with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Output Caching with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 55
    2005-01-11


    Table of Contents:
  • Output Caching with PHP
  • Capturing Server Side Output
  • Output Buffering for Server Side Caching
  • Multiple caching: splitting the Web page content
  • Putting it All Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Output Caching with PHP - Multiple caching: splitting the Web page content
    ( Page 4 of 5 )

    As stated above, since most websites are composed of a header section, body and footer section, we are going to implement server side caching for each part. Also, we need to set up some kind of criteria for checking whether cache files are valid. The most common caching policy approaches are the following:

    • Time triggered caching (expiry timestamp).

    • Content change triggered caching (sensitive content has changed, so cache must be updated).

    • Manually triggered caching (manually inform the application that information is outdated, and force a new cache creation).

    For the scope of this article, we will use a time triggered caching policy for each part of the web page.

    Let’s see how it works:

    First, we define that the page header will have an expiry time of one day or 86400 seconds (only for purposes of example). Our body section will have an expiry time of ten seconds, and our page footer will be valid for one day.

    Having defined expiry times for each section, let’s build two very intuitive and useful functions. The first one will take care of creating the cache files for the proper section. The second will check to see whether there is a valid cached version for the section. If there is, it will return the cached content extracted from the cache file. Otherwise, it will return false.

    The functions are as follow:

    function createCache ( $content ,  $cacheFile ) {
      $fp = fopen( $cachefile , ‘w’ );
      fwrite( $fp , $content );
      fclose( $fp);
    }

    function getCache ( $cacheFile ,  $expireTime ) {
      if ( file_exists ( $cacheFile ) && filemtime ( $cacheFile ) >( time() - $expireTime ) ) {
        return file_get_contents( $cacheFile );
      }
      return false;
    }

    The function createCache takes two parameters: content from the output buffer and the cache filename where the output will be stored. Once the buffer content is obtained, it is stored as a file. Similarly, the function getCache accepts two arguments: the cache filename and expiry time (expressed in seconds). The function checks to see whether there is a cache file and whether it is valid. Cache validity is tested to see whether the cache file modification timestamp is greater than the current time less the expiry time. If the cache file is found to be valid, then cache content is returned. Otherwise it returns false.

    Having defined this two core functions, we are able to build the new script:

    <?php
    //   start output buffering
    ob_start();

    //  check if a valid header cache exists
    if ( !$header = getCache( ‘headerCache.txt’ , 86400 ) {

    //  display header section
    ?>

    <html>
    <head>
    <title>Cached Page</title>
    </head>
    <body>
    The header section is updated on a daily basis.

    <?php
    $header = ob_get_contents();
    ob_clean();
    createCache( $header , ‘headerCache.txt’ );

    }

    // check if a valid body cache exists
    if ( !$body = getCache( ‘bodyCache.txt’ , 10 ) {

    //  display body section
    ?>

    <h1>This section is updated every 10 seconds.</h1>

    <?php
    $body = ob_get_contents();
    ob_clean();
    createCache( $body , ‘bodyCache.txt’ );

    }

    // check if a valid footer cache exists
    if ( !$footer = getCache( ‘footerCache.txt’ , 86400 ) {

    //  display footer section
    ?>
    The footer section is updated on a daily basis.
    </body>
    </html>

    <?php
    $footer = ob_get_contents();
    ob_clean();
    createCache( $footer , ‘footerCache.txt’ );

    }

    //   stops output buffering
    ob_end_clean();

    //   display the complete page

    echo $header .  $body .  $footer;

    ?>

    The script is pretty straightforward in its functionality. Let’s see what it does in detail.

    Once output buffering is started, the script checks in turn for the corresponding cache files, which store header, body and footer sections. If the cache files are not valid based on expiry times given for each section, new content is generated, stored in the buffer and finally written to the proper cache files. When the whole page content is obtained either from the buffer or from cache files, it will be displayed in the user’s browser.

    The script’s output is the following:

    The header section is updated on a daily basis.

    This section is updated every 10 seconds.

    The footer section is updated on a daily basis.

    Each section of the page will be updated according to our caching policy, with the body updated every ten seconds.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT