PHP
  Home arrow PHP arrow Page 4 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 / 18
    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 - Digging Deeper


    (Page 4 of 9 )

    When implementing a cache for your Web page, the first step is, obviously, to include the Cache_Lite class

    
    

    <?php


    // include the package

    require_once("Lite.php");


    ?>

    You can either provide an absolute path to this file, or do what most lazy programmers do - include the path to your PEAR installation in PHP's "include_path" variable, so that you can access any of the PEAR classes without needing to type in long, convoluted file paths.

    The Cache_Lite object can support multiple caches simultaneously, so long as every cache created has a unique identifier. In this case, I've used the identifier "starwars" to uniquely distinguish the cache I'll be using.

    
    

    <?php


    // set an ID for this cache

    $id = "starwars";


    ?>

    Next, an object of the Cache_Lite class needs to be initialized, and assigned to a PHP variable.

    
    

    <?php


    // create a Cache_Lite object

    $objCache = new Cache_Lite($options);


    ?>

    This variable, $objCache, now serves as the control point for cache manipulation.

    The constructor of the Cache_Lite class can be provided with an associative array containing configuration parameters; these parameters allow you to customize the behaviour of the cache. In the example above, this array contains two parameters, "cacheDir", which specifies the directory used by the cache, and "lifeTime", which specifies the period for which data should be cached, in seconds.

    
    

    <?php


    // set some variables

    $options = array(

    "cacheDir" => "cache/",

    "lifeTime" => 50

    );


    ?>

    Note that the directory specified must already exist, or else the cache will simply not work.

    Once an instance of the Cache_Lite object has been created, the business logic to use it becomes fairly simple. The first step is to check if the required data already exists in the cache. If it doesn't, it should be generated from the original data source, and a copy saved to the cache for future use. If it does, you can do something useful with it - write it to a file, pipe it to an external program or - as I've done here - simply output it to the screen for all to admire.

    
    

    <?php


    // test if there exists a valid cache

    if ($quote = $objCache->get($id))

    {

    // if so, display it

    echo $quote;



    // add a message indicating this is cached output

    echo " [cached]";

    }

    else

    {

    // no cached data

    // implies this data has not been requested in last cache lifetime

    // so obtain it and display it

    $quote = "Do, or do not. There is no try. -- Yoda, Star Wars";

    echo $quote;



    // also save it in the cache for future use

    $objCache->save($quote, $id);

    }


    ?>

    Most of this logic is accomplished via the get() and save() methods of the Cache_Lite object. The get() method checks to see if the data exists in the cache and returns it if so, while the save() method saves data to the cache. The save() method accepts the data to be saved, together with a unique identifier, as input arguments; the get() method uses this identifier to find and retrieve the cached data.

    The steps above make up a fairly standard process for using the Cache_Lite class, and you'll see them being repeated over and over again in subsequent examples as well.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway