Karakas Online

24.1.2. Cache-Lite

Figure 24-4. Cache-Lite is powered by PEAR.

Cache-Lite is powered by PEAR.

Cache-Lite is powered by PEAR.



After reading the above, you may ask yourself if this is all we can do to accelerate PHP-Nuke with caching - and the answer is: of course not!

jpcache (Section 24.1.1) enables caching of the whole page. Remember, we edited index.php to set a different cache expiration time for logged and not logged-in users. The idea behind this was that a user that is not logged in will not have access to most of the dynamic features of PHP-Nuke, thus making caching of the whole page for longer intervalls an acceptable compromise between freshness of content and speed of page generation.

But how about our logged-in users? Wouldn't it be nice if we could at least cache some PHP-Nuke blocks we know they don't change that often? This is indeed possible with Cache-Lite of the PEAR library from PHP.

24.1.2.1. Installation of Cache-Lite

Download Cache-Lite and extract it in the includes folder of your PHP-Nuke installation.

If your PHP does not include the PEAR library, you will need to download download the base PEAR package too. Extract only the PEAR.php file and put it in the same folder as mainfile.php, i.e. your PHP-Nuke base folder.

Cache-Lite needs the PEAR.php file for its error-handling, so you may not notice that you need it until some error occurs. wink

If your PHP installation includes PEAR, you don't need to bother installing PEAR.php anywhere - it is already there, at the right place. But if your ISP does not include it, the above PEAR.php will do the trick.

That's all! You are now ready to use Cache-Lite.

24.1.2.2. How to use Cache-Lite

Cache-Lite can easily be used to cache a PHP variable. The easiest application of this is to cache the $content variable of a block that does not change very often - and this is the majority of PHP-Nuke blocks!

To use Cache-Lite in a PHP-Nuke block, open that block in your editor and put the following code (see How to accelerate PHP-Nuke):

// include the Cache-Lite package
require_once("includes/Cache_Lite/Lite.php");
// set some variables
$options = array(
  "cacheDir" => "/tmp/Cache_Lite/",
  "lifeTime" => 900
);
// create a Cache_Lite object
$objCache = new Cache_Lite($options);
// test if there exists a valid cache,
// the <acronym>ID</acronym> will be the basename of the block
$fileid = basename($blockfile,".php");
if ($content = $objCache->get($fileid)) {
// add a message indicating this is cached output
  $content .= "\n[cached with ID=$fileid]";
}
else
{
// do the block's work here...

after the lines

if (eregi("block-XXXXXXX.php",$PHP_SELF)) {
  Header("Location: index.php");
  die();
}

What does the inserted code do?

Of course, we are not done yet - after we compute $content in our block, we will want to cache it for future use. wink

Thus, after the end of your computations and before you close the block with "?>", put the following to instruct Cache-Lite to cache your $content:

// ...and save it in the cache for future use
$objCache->save($content, $fileid);
}

This closes the IF-statement we opened with the previous block of lines. It caches the $content variable in our cache object, using as identifier the file ID we derived from the block's filename. You have to put these two blocks of lines in every PHP-Nuke block you plan to cache, but the work is worth it!

The complete code for your cached block should thus look like:

<?php
if (eregi("block-XXXXXXX.php",$PHP_SELF)) {
    Header("Location: index.php");
    die();
}
// include the Cache-Lite package
require_once("includes/Cache_Lite/Lite.php");
// set some variables
$options = array(
  "cacheDir" => "/tmp/Cache_Lite/",
  "lifeTime" => 900
);
// create a Cache_Lite object
$objCache = new Cache_Lite($options);
// test if there exists a valid cache,
// the <acronym>ID</acronym> will be the basename of the block
$fileid = basename($blockfile,".php");
if ($content = $objCache->get($fileid)) {
  // add a message indicating this is cached output
  $content .= "\n[cached with ID=$fileid]";
}
else
{
  // do the whole work here...
-------> Your original code goes here <-------
  // ...and save it in the cache for future use
  $objCache->save($content, $fileid);
}
?>

If something goes wrong and your block is not being cached, check the following (How to accelerate PHP-Nuke):

For a more in-depth tutorial on Cache-Lite, see Caching With PHP Cache_Lite.

You can use both jpcache and Cache-Lite together in a cache strategy that caches the whole page with jpcache when a user is not logged-in, but caches the least changing blocks (probably most of them) with Cache-Lite when he is. Such a strategy allows enough flexibility and room for experimentation and brings a considerable acceleration to your PHP-Nuke, without, on the other hand, requiring any access to the internals of your PHP installation - something that you will need, if you want to try the ultimate approach to caching , which we will present you in Section 24.1.3.

Last updated Sun Aug 7 21:14:12 CEST 2005 Permalink: http://www.karakas-online.de/EN-Book/cache-lite.html All contents © 2004 Chris Karakas, Claudio Erba