Using PEAR Cache_Lite

Cache_Lite is a PHP PEAR package designed for caching stuff. As the project description puts is:

This package is a little cache system optimized for file containers. It is fast and safe (because it uses file locking and/or anti-corruption tests).

The documentation for this project is actually quite good (I’m not impressed by the level of documentation for the majority of PEAR packages), so I would suggest you pop over there and take a look at it.

Anyway, I’ve just written a little bit of code to get the Word of the Day and cache it for two hours, so I thought I’d share it here. The script also uses the XML_RSS package, which is a handy little tool when you’re dealing with RSS.

** This script demonstrates some simple useage of two PHP PEAR packages
** (Cache_Lite and XML_RSS).
** When run it displays the Word of the Day. First of
** all, it looks to see if we have a local cached copy of the Word of
** the Day. If not, it gets the Word of the Day RSS feed from
**, pulls out the current Word of the Day, and then
** caches it locally ready for use the next time.

// include the PEAR packages

// name a cache key
$cache_id = 'wotd';

// the data (an array) that we want to cache
$data = array();

// Set a few Cache_Lite options
$options = array(
    'cacheDir' => '/tmp/',      // must include trailing slash…
    'lifeTime' => 7200,              // cache life in seconds (2 hours)
    'pearErrorMode' => CACHE_LITE_ERROR_DIE  // helps us when debugging
                                    // but not good for production site

// Create a Cache_Lite object
$cache = new Cache_Lite($options);

// Test if there is a valide cache item for this data
if( $data = $cache->get($cache_id) )
    // Cache hit
    $data = unserialize($data);
   // Cache miss
   $rss =& new XML_RSS('');
   foreach ($rss->getItems() as $item)
      $data['link'] = $item['link'];
      list($data['title'], $tmp) = explode(':', $item['title']);
      // we only need to get the first item, so jump out here

// print out the results of our work!
echo 'Word of the day: <a href="' . $data['link'] . '">' . $data['title'] . '</a>';


So there you have it!

It’s pretty simple really, but the package looks to be quite powerful. I’m sure the other caching package, PEAR Cache is good too, but the documentation here is rather poor :-(

Sociable:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • digg
  • Furl
  • NewsVine
  • Reddit
  • YahooMyWeb

2 Responses to “Using PEAR Cache_Lite

  • Fabien
    June 20th, 2005 20:19


    I’m the main author of Cache_Lite.

    Juste a few remarks :

    - move the line “require_once(’XML/RSS.php’);” inside the “cache miss” section and not on the top of the code

    => IMHO, perfs will be twice better

    - to simplify, you can use the “automaticSerialization” option to avoid the serialize()/unserialize() process in your code (because in this case, Cache/Lite will deal with it)


    PS : Your article is quite good :-)

  • marky moo
    June 20th, 2005 21:07

    That’s a good point about moving the line require_once('XML/RSS.php'); - especially if you are after performance.

    Once again, congratulations on an excellent package!

Leave a Reply