Introduction

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):

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.

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?