PEAR Articles
  Home arrow PEAR Articles arrow Page 4 - Caching with PEAR::Cache
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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? 
PEAR ARTICLES

Caching with PEAR::Cache
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2004-03-19

    Table of Contents:
  • Caching with PEAR::Cache
  • Installing Pear::Cache
  • Defining the Cache
  • Implementing a Framework
  • Combined Code
  • Conclusion

  • 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 PEAR::Cache - Implementing a Framework


    (Page 4 of 6 )

    The generateID method creates an md5 hash based on a given value. The hash can be used to identify the cached script results and is passed to the get and save methods when retrieving and storing the data.

    <?php
    $cached_id 
    $cache-&gt;generateID($_SERVER["PHP_SELF"]);
    ?>

    The save method accepts a minimum of two arguments: the id and the contents to store in the cache. However, information that changes must be regenerated and recached. A number representing the length of time for which the cached information is to be considered valid may be passed as a third argument (the time is measured in seconds). For example, the following example caches information with a shelf-life of 24 hours:

    <?php
    // 60 seconds Ã— 60 minutes Ã— 24 hours = 86400 seconds/day
    $cache-&gt;save($cached_id$content86400);
    ?>

    If the cached data's life is not set then it will never expire.

    Output buffering can be used to capture a script's output for storage as it is being generated. If you're not already familiar with the output buffering functionality provided by PHP, I recommend Michael Bailey's excellent tutorial entitled PHP Output Buffering.

    <?php
    ob_start
    ();
    // ...PHP script which results in output goes here.
    $content ob_get_contents();
    ob_end_clean();
    $cache-&gt;save($cached_id$content3600); //cache for 1 hour
    ?>

    The get method accepts an id and returns the cached content if it exists. If the content doesn't exist or has expired it will return null.

    <?php
    if ($content $cache-&gt;get($cached_id))
        echo 
    $content;
    ?>

    More PEAR Articles Articles
    More By bluephoenix


       · For all my proficiency with PHP the need to do command line stuff has so far passed...
       · Dear AllI have defined variables as follow in an data entry formprod1...
       · Dear AllI have defined variables as follow in an data entry formprod1...
       · Essentially all of PHP setup and maintenance is conducted on a command line. You'll...
       · This type of question should really be posed in the forum. There are many people...
       · &quot;Net_Socket is part of the default Pear installation so...&quot;...
       · study
       · i think it's a case to case basis Jayden. what platform are you using?
     

    PEAR ARTICLES ARTICLES

    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning
    - Using XML_RPC2 with PEAR
    - Using Web Service APIs (Amazon and Yahoo!) w...
    - Database Abstraction with MDB2 from PEAR
    - The PEAR Package Tour: PEAR Basics
    - Caching with PEAR::Cache
    - Introduction to PEAR
    - The PEAR ITX Templating System





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