Jaslabs: High performance Software

High Performance Software

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’ =&gt ‘/cachetmp’, //this is the temp directory of the cache files
‘lifeTime =&gt 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-&gtget($cacheid)) {

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

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

$Cache_Lite-&gtsave($data);

}

?>

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

Example:

&lt?php
require_once(”Cache/Lite.php”);
// (…)
$cache_object = new Cache_Lite();
if ($data = $cache_object-&gtget($cache_id)) { // cache hit !
echo($data);
} else { // page has to be constructed
require_once(”…”)
require_once(”…”)
// (…)
$Cache_object-&gtsave($data);
}
?&gt

Downloading

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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

No comments yet. Be the first.

Leave a reply