PEAR Articles

  Home arrow PEAR Articles arrow Page 4 - Caching with PEAR::Cache
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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    PEAR ARTICLES ARTICLES

    - Installing PEAR
    - PEAR: an Introduction
    - Managing robots.txt using PHP: Generating Dy...
    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap