spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / phpanth3 / 1 To page 1To page 2To page 3To page 4current pageTo page 6To page 7
[previous] [next]

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


PHP Anthology, Volume 2: Applications. Chapter 5: Caching

How do I implement a simple server side caching system?

Now that we have a grasp of the ideas behind output buffering, it’s time to see how we can put this process into action in a manner that will be easy to maintain. To do this, we’ll use a little help from PEAR::Cache_Lite (version 1.1 was used in the examples here).

As I mentioned, in the interests of keeping your code maintainable and having a reliable caching mechanism, it’s a good idea to delegate the responsibility of caching logic to classes you trust. Cache_Lite provides a solid but easy to use library for caching, handling issues such as file locking, creating, checking for, and deleting cache files, controlling the output buffer, and directly caching the results from function and class method calls. More to the point, Cache_Lite should be relatively easy to apply to an existing application, requiring only minor code modifications.

There are three main classes in Cache_Lite. First is the base class, Cache_Lite, which deals purely with creating and fetching cache files, but makes no use of output buffering. This class can be used alone for caching operations in which you have no need for output buffering, such as storing the contents of a template you’ve parsed with PHP. The examples here will not use Cache_Lite directly, but will instead focus on the two subclasses. Cache_Lite_Function can be used to call a function or class method and cache the result; this might prove useful for storing a MySQL query result set, for example. The Cache_Lite_Output class uses PHP’s output control functions to catch the output generated by your script, and store it in cache files; it allows you to perform tasks such as those we completed in the previous solution.

Here’s an example of how you might use Cache_Lite to accomplish the task we completed in the last solution. When instantiating any of Cache_Lite’s classes, we must first provide an array of options that determine the behavior of Cache_Lite. We’ll look at these in detail in a moment. Note that the cacheDir directory specified must be one to which the script has read and write access.

Example 5.7. 4.php (excerpt)

<?php
// Include the PEAR::Cache_Lite Output class
require_once 'Cache/Lite/Output.php';

// Define options for Cache_Lite
$options = array(
  'cacheDir'        => './cache/',
  'writeControl'    => 'true',
  'readControl'     => 'true',
  'readControlType' => 'md5'
);

// Instantiate Cache_Lite_Output
$cache = new Cache_Lite_Output($options);

For each chunk that we want to cache, we need to set a lifetime (in seconds) for which the cache should live before it’s refreshed. Next, we use the start method, available only in the Cache_Lite_Output class, to turn on output buffering. The two arguments passed to the start method are an identifying value for this particular cache file, and a cache group. This is an identifier that allows a collection of cache files to be acted upon; it’s possible to delete all cache files in a given group, for example (more on this in a moment). Once the output for this chunk has finished, we use the end method to stop buffering and store the content as a file.

Example 5.8. 4.php (excerpt)

// Set lifetime for this "chunk"
$cache->setLifeTime(604800);

// Start the cache with an id and group for this chunk
if (!$cache->start('header', 'Static')) {
  ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <title> PEAR::Cache_Lite example </title>
  <meta http-equiv="Content-Type"
    content="text/html; charset=iso-8859-1" />
  </head>
  <body>
  <h2>PEAR::Cache_Lite example</h2>
  The header time is now: <?php echo date('H:i:s'); ?><br />
  <?php
  // Stop and write the cache
  $cache->end();
}

Caching the body and footer follows the same procedure as the header. Note that we again specify a five second lifetime when caching the body:

Example 5.9. 4.php (excerpt)

$cache->setLifeTime(5);
if (!$cache->start('body', 'Dynamic')) {
  echo 'The body time is now: ' . date('H:i:s') . '<br />';
  $cache->end();
}

$cache->setLifeTime(604800);
if (!$cache->start('footer', 'Static')) {
  ?>
  The footer time is now: <?php echo date('H:i:s'); ?><br />
  </body>
  </html>
  <?php
  $cache->end();
}
?>

On viewing the page, Cache_Lite creates in the cache directory files with these names:

./cache/cache_Static_header
./cache/cache_Dynamic_body
./cache/cache_Static_footer

When the same page is requested later, the code above will use the cached file if it is valid and has not expired.

Protect your Cache Files

Make sure that the directory in which you place the cache files is not publicly available, or you may be offering your site’s visitors access to more than you realize.

Cache_Lite Options

When instantiating Cache_Lite (or any of its subclasses, such as Cache_Lite_Output), there are a number of ways to control its behavior. These should be placed in an array and passed to the constructor as in the previous example:

Example 5.10. 4.php (excerpt)

// Define options for Cache_Lite
$options = array(
  'cacheDir'        => './cache/',
  'writeControl'    => TRUE,
  'readControl'     => TRUE,
  'readControlType' => 'md5'
);

// Instantiate Cache_Lite_Output
$cache = new Cache_Lite_Output($options);

In the current version (1.1) the available options are:

cacheDir

This is the directory in which the cache files will be placed. This defaults to the current script execution directory.

caching

This option switches on or off the caching behavior of Cache_Lite. If you have numerous Cache_Lite calls in your code and want to disable the cache for debugging, for example, this will be important. The default value is TRUE (caching enabled).

lifetime

This represents the default lifetime (in seconds) of cache files. It can be changed using the setLifeTime method. The default value is 3600 (one hour).

fileNameProtection

With this option activated, Cache_Lite uses an MD5 encryption hash to generate the filename for the cache file. This protects you from error when you try to use IDs or group names containing characters that aren’t valid for filenames; it must be turned on when you use Cache_Lite_Function. The default is TRUE (enabled).

fileLocking

This is used to switch the file locking mechanisms on or off. The default is TRUE (enabled).

writeControl

This checks that a cache file has been written correctly immediately after it has been created, and throws a PEAR::Error if it finds a problem. Obviously, this would allow your code to attempt to rewrite a cache file that was created incorrectly, but comes at a cost in terms of performance. The default is TRUE (enabled).

readControl

This checks cache files that are being read for corruption. Cache_Lite is able to place inside the file a value, such as the string length of the file, which can be used to confirm that the cache file isn’t corrupted. There are three alternative mechanisms for checking that a file is valid, and they’re specified using the readControlType option. These mechanisms come at the cost of performance, but should help guarantee your visitors aren’t seeing scrambled pages. The default value is TRUE (enabled).

readControlType

This specifies the type of read control mechanism to use. The available mechanisms are a cyclic redundancy check ('crc32', the default value) using PHP’s crc32 function, an MD5 hash using PHP’s md5 function ('md5'), or a simple and fast string length check ('strlen'). Note that this mechanism is not intended to provide security from people tampering with your cache files; it’s just a way to spot corrupt files.

pearErrorMode

This tells Cache_Lite how it should return PEAR errors to the calling script. The default is CACHE_LITE_ERROR_RETURN, which means Cache_Lite will return a PEAR::Error object

memoryCaching

With memory caching enabled, every time a file is written to the cache, it is stored in an array in Cache_Lite. The saveMemoryCachingState and getMemoryCachingState methods can be used to store and access the memory cache data between requests. The advantage of this is that the complete set of cache files can be stored in a single file, reducing the number of disk read/writes by reconstructing the cache files straight into an array to which your code has access. We’ll be sticking to the normal Cache_Lite mechanism here, but memoryCaching may be worth further investigation if you run a large site. The default value is TRUE (disabled).

onlyMemoryCaching

If this is enabled, only the memory caching mechanism will be used. The default value is TRUE (disabled).

memoryCachingLimit

This places a limit on the number of cache files that will be stored in the memory caching array. The more cache files you have, the more memory will be used up by memory caching, so it may be a good idea to enforce a limit that prevents your server from having to work too hard. Of course, this places no restriction on the size of each cache file, so just one or two massive files may cause a problem. The default value is 1000.

home / programming / phpanth3 / 1 To page 1To page 2To page 3To page 4current pageTo page 6To page 7
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27 2003
Revised: January 2, 2004

URL: http://webreference.com/programming/phpanth3