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
$Cache_Lite->save($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:
<?php
require_once("Cache/Lite.php");
// (...)
$cache_object = new Cache_Lite();
if ($data = $cache_object->get($cache_id)) { // cache hit !
echo($data);
} else { // page has to be constructed
require_once("...")
require_once("...")
// (...)
$Cache_object->save($data);
}
?>
Downloading
Cache_lite is part of the free pear package, which is a large repository of modules available Here

0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home