Strict Standards: Redefining already defined constructor for class wpdb in /var/www/www.sweeting.org/html/mark/blog/wp-includes/wp-db.php on line 52

Deprecated: Assigning the return value of new by reference is deprecated in /var/www/www.sweeting.org/html/mark/blog/wp-includes/cache.php on line 36

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /var/www/www.sweeting.org/html/mark/blog/wp-includes/cache.php on line 389

Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 537

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 537

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 537

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 537

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 556

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 653

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 653

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 653

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 653

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el($output) in /var/www/www.sweeting.org/html/mark/blog/wp-includes/classes.php on line 678

Deprecated: Assigning the return value of new by reference is deprecated in /var/www/www.sweeting.org/html/mark/blog/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /var/www/www.sweeting.org/html/mark/blog/wp-includes/theme.php on line 507
Using PEAR Cache_Lite
Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method GeoURL::tags() should not be called statically in /var/www/www.sweeting.org/html/mark/blog/wp-includes/plugin.php on line 160

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 dictionary.com 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.

<?php
/*
** This script demonstrates some simple useage of two PHP PEAR packages
** (Cache_Lite and XML_RSS).
**
** When run it displays the dictionary.com 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
** dictionary.com, pulls out the current Word of the Day, and then
** caches it locally ready for use the next time.
*/

// include the PEAR packages
require_once('XML/RSS.php');
require_once('Cache/Lite.php');

// 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);
}
else
{
   // Cache miss
   $rss =& new XML_RSS('http://dictionary.reference.com/wordoftheday/wotd.rss');
   $rss->parse();
   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
      break;
   }
   $cache->save(serialize($data));
}

// 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.
  • del.icio.us
  • digg
  • Furl
  • NewsVine
  • Reddit
  • YahooMyWeb

2 Responses to “Using PEAR Cache_Lite

  • Fabien
    June 20th, 2005 20:19
    1

    Hi,

    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)

    Regards

    PS : Your article is quite good :-)

  • marky moo
    June 20th, 2005 21:07
    2

    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