Tuesday, February 28, 2006

using pear cache_lite for high performance

By Justin Silverton

What is cache lite?

Cache_lite is a php/pear caching module that is designed for high traffic sites. It is different than most caching systems because it has a built-in locking mechanism that will prevent cache corruption that can sometimes occur when there are a large amount of concurrent users trying to read and write to your cached data.

How to use it

Here is a simple example on how to cache your data:

//assign an id to the cache object
$cacheid = '589';

$cache_options = array(
'cacheDir' => '/cachetmp', //this is the temp directory of the cache files
'lifeTime => 5000 //time, in seconds, of how long the cache will be valid

// Create a Cache_Lite object
$Cache_object = new Cache_Lite($cache_options);

// Test if thereis a valide cache for this id
if ($data = $Cache_object->get($cacheid)) {

// data is in our cache, access it through $data

} else { // Not found in cache, it needs to be added




Performance tip: Don't include every package your page needs. Only
load the modules you need when the page is not in the cache.


// (...)
$cache_object = new Cache_Lite();
if ($data = $cache_object->get($cache_id)) { // cache hit !
} else { // page has to be constructed
// (...)


Cache_lite is part of the free pear package, which is a large repository of modules available Here


Post a Comment

Links to this post:

Create a Link

<< Home