PHP
  Home arrow PHP arrow Page 5 - Caching With PHP Cache_Lite
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Caching With PHP Cache_Lite
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2003-06-06

    Table of Contents:
  • Caching With PHP Cache_Lite
  • The Food Chain
  • Return Of The Jedi
  • Digging Deeper
  • In And Out
  • Bits And Bytes
  • No News Is Good News
  • Cache Cow
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Caching With PHP Cache_Lite - In And Out


    (Page 5 of 9 )

    The example you just saw cached the output of PHP statements. It's also possible to cache static HTML content, through creative use of PHP's output buffering functions. Consider the following example, which revises the code on the previous page to cache HTML markup instead of PHP command output:

    
    

    <?php


    // include the package

    require_once("Lite.php");


    // set some variables

    $options = array(

    "cacheDir" => "cache/",

    "lifeTime" => 5

    );


    // create a Cache_Lite object

    $objCache = new Cache_Lite($options);


    // test if there exists a valid cache

    if ($page = $objCache->get("starwars"))

    {

    // if so, display it

    echo $page;



    // add a message indicating this is cached output

    echo " [cached]";

    }

    else

    {

    // no cache

    // so display the HTML output

    // and save it to a buffer

    ob_start();


    ?>

    <html>

    <head></head>

    <body>

    Do, or do not. There is no try.

    <br>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- Yoda,
    <i>Star Wars</i> </body> </html>


    <?php


    // page generation complete

    // retrieve the page from the buffer

    $page = ob_get_contents();



    // and save it in the cache for future use

    $objCache->save($page, "starwars");


    // also display the buffer and then flush it

    ob_end_flush();


    }


    ?>

    In this case, PHP's output buffering functions have been used to capture all the output generated subsequent to the call to ob_start(), and store this output in a buffer. Once the entire output has been captured, the
    ob_get_contents() function is used to retrieve the buffer contents to a variable. This variable (which now contains the entire HTML page) is then stored in the cache for future use. Finally, the ob_end_flush() function is used to end output buffering and send the contents of the buffer to the browser.

    {mospagebreak title=Different Strokes}

    An alternative method to accomplish the task on the previous page lies with the Cache_Lite_Output() class, a subclass of the Cache_Lite class. This subclass (which internally uses output buffering) inherits all of the attributes of the parent class, and adds two methods to the parent class' method collection: a start() method, which begins caching data, and an
    end() method, which marks the end of data caching.

    Consider the following variant of the example on the previosu page, which illustrates how this may be used:

    
    

    <?php


    // include the package

    require_once("Output.php");


    // set some variables

    $options = array(

    "cacheDir" => "cache/",

    "lifeTime" => 5

    );


    // create a Cache_Lite_Output object

    $objCache = new Cache_Lite_Output($options);


    // test if there exists a valid cache

    // if so, display it, else regenerate the page

    if (!$objCache->start("starwars"))

    {

    ?>


    <html>

    <head></head>

    <body>

    Do or do not. There is no try.

    <br>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- Yoda,
    <i>Star Wars</i> </body> </html>


    <?php

    // end caching

    $objCache->end();

    }

    ?>

    Over here, if the call to start() - which is provided with a cache identifier - returns true, it implies that the requested data has already been cached, and the Cache_Lite_Output object takes care of printing the contents of the cache. If, however, the call to start() returns false, it implies that the data is not present in the cache, and so all subsequent output generated by the script will be cached, until the object's end() method is invoked.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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