Introduction

Introduction – Introduction to Cache_Lite

Description

PEAR::Cache_Lite is a little cache system. It's optimized for high traffic websites so it is really fast and safe (because it uses file locking and/or anti-corruption tests).

Note : An independent documentation of Cache_Lite is available in chinese on this page.

Goals and technical details

speed

Before all, PEAR::Cache_Lite has to be extremely fast. That returns the use of PEAR possible on sites with high traffic without falling in hi-level hardware solutions.

You can find more details about cache_lite technical choices on this document, but the main idea is to include PEAR.php file ONLY when an error occurs (very rare).

simplicity

Because the cache system is often embedded in the application layer, PEAR::Cache_Lite kernel has to be small and flexible with an adapted licence (LGPL). Advanced functionality can be found in files that extend the core.

security

On high traffic websites, the cache system has to be really protected against cache files corruptions (because of concurrent accesses in read/write mode). Very few cache systems offer a protection against this problem.

File locking is not a perfect solution because it is useless with NFS or multithreaded servers. So in addition to it, PEAR::Cache_Lite offers two fully transparent mechanisms (based on hash keys) to guarantee the security of the data in all circumstances. Just have a look at the link given in the previous paragraph for more details.

Usage

general usage

Every module of Cache_Lite follows the same philosophy.

Parameters (others than default ones) are passed to the constructor by using an associative array.

A cache file is identified by a cache ID (and eventually a group). For obvious flexibility reasons, the logic of IDs choice is left to the developer.

In the following, we will use the term "group" as a pool of cache files and the term "block" as a piece of an HTML page.

core

Let's start with a simple example : the page is built, then recovered with a unique variable (string):

<?php

// Include the package
require_once('Cache/Lite.php');

// Set a id for this cache
$id '123';

// Set a few options
$options = array(
    
'cacheDir' => '/tmp/',
    
'lifeTime' => 3600
);

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

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

    
// Cache hit !
    // Content is in $data
    // (...)

} else { // No valid cache found (you have to make the page)

    // Cache miss !
    // Put in $data datas to put in cache
    // (...)
    
$Cache_Lite->save($data);

}

?>

If you wish use a cache per block and not a global cache, take as example the following script:

<?php
require_once('Cache/Lite.php');

$options = array(
    
'cacheDir' => '/tmp/',
    
'lifeTime' => 3600
);

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

if (
$data $Cache_Lite->get('block1')) {
    echo(
$data);
} else {
    
$data 'Data of the block 1';
    
$Cache_Lite->save($data);
}

echo(
'<br><br>Non cached line !<br><br>');

if (
$data $Cache_Lite->get('block2')) {
    echo(
$data);
} else {
    
$data 'Data of the block 2';
    
$Cache_Lite->save($data);
}

?>

core

However, it is not always possible to recover all the contents of a page in a single string variable. Thus Cache_Lite_Output comes to our aid :

<?php

require_once('Cache/Lite/Output.php');

$options = array(
    
'cacheDir' => '/tmp/',
    
'lifeTime' => 10
);

$cache = new Cache_Lite_Output($options);

if (!(
$cache->start('123'))) {
    
// Cache missed...
    
for($i=0;$i<1000;$i++) { // Making of the page...
        
echo('0123456789');
    }
    
$cache->end();
}

?>

The idea is the same for a per block usage :

<?php

require_once('Cache/Lite/Output.php');

$options = array(
    
'cacheDir' => '/tmp/',
    
'lifeTime' => 10
);

$cache = new Cache_Lite_Output($options);

if (!(
$cache->start('block1'))) {
    
// Cache missed...
    
echo('Data of the block 1 !');
    
$cache->end();
}

echo(
'Non cached line !');

if (!(
$cache->start('block2'))) {
    
// Cache missed...
    
echo('Data of the block 2 !');
    
$cache->end();
}

?>

IMPORTANT NOTE

For a maximum efficiency with Cache_Lite, do not systematically include every package the page needs. ONLY load the modules you need when the page is not in the cache (and has to be recomputed) by using a conditional inclusion.

The BAD way :

<?php
   
require_once("Cache/Lite.php");
   require_once(
"...")
   require_once(
"...")
   
// (...)
   
$cache = new Cache_Lite();
   if (
$data $Cache_Lite->get($id)) { // cache hit !
       
echo($data);
   } else { 
// page has to be (re)constructed in $data
       // (...)
       
$Cache_Lite->save($data);
   }
?>

Here is the good way (often more than twice faster) :

<?php
   
require_once("Cache/Lite.php");
   
// (...)
   
$cache = new Cache_Lite();
   if (
$data $Cache_Lite->get($id)) { // cache hit !
       
echo($data);
   } else { 
// page has to be (re)constructed in $data
       
require_once("...")
       require_once(
"...")
       
// (...)
       
$Cache_Lite->save($data);
   }
?>

Conclusion

To go further with Cache_Lite, have a look at examples and technical details bundled with the package.

Cache_Lite (Previous) Constructor (Next)
Last updated: Wed, 22 Oct 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

Note by: joei007 at gmail dot com
Hi, andrea@gianfreda.com

You should remove cache associate with your data update to database.

mysql_query('update blah blah....');
$cache->remove('id_of_content');

or

wait until cache expire.
Note by: andrea@gianfreda.com
Hi,

i would like to ask you how to force the generation of cache files with a trigger: for example, i update a table in a database. A cached content is yet active, but i would like to refresh it. How can i do it with cache_lite?

Thanks,
Andrea