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

Caching With PHP Cache_Lite
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    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:
      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


    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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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