PHP
  Home arrow PHP arrow Page 5 - 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 - Putting it All Together
    ( Page 5 of 5 )

    Here is the complete script:

    <?php

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

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

    //   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;

    ?>

    Conclusion

    That’s the general idea behind the concept of caching server side output. Major performance improvements can be achieved if used in conjunction with proper caching policies for specific sections of the website.

    Since I am a strong advocate of object oriented programming, I would recommend using some good and trusted caching classes, such as Pear::Cache_Lite, in order keep your code maintainable and have a reliable caching mechanism for websites.

    From this point, there is long way to go. Caching is a very huge subject, and it can be approached from several points. But one thing is certain: caching server output with PHP output buffering functions is a good addition to your toolbox when building dynamic websites. Good luck!



     
     
    >>> 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 1 hosted by Hostway
    Stay green...Green IT