Jaslabs: High performance Software

High Performance Software

Archive for the 'pear' Category

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’ =&gt ‘/cachetmp’, //this is the temp directory of the cache files
‘lifeTime =&gt 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-&gtget($cacheid)) {

// data is in our cache, access it through $data

} else { // Not found in cache, it needs to be added

$Cache_Lite-&gtsave($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:

&lt?php
require_once(”Cache/Lite.php”);
// (…)
$cache_object = new Cache_Lite();
if ($data = $cache_object-&gtget($cache_id)) { // cache hit !
echo($data);
} else { // page has to be constructed
require_once(”…”)
require_once(”…”)
// (…)
$Cache_object-&gtsave($data);
}
?&gt

Downloading

Cache_lite is part of the free pear package, which is a large repository of modules available Here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
No comments

Using PEAR cache

The PEAR Cache is a set of caching classes that allows you to cache multiple types of data, including HTML and images.

The most common use of the PEAR Cache is to cache HTML text. To do this, we use the Output buffering class which caches all text printed or echoed between the start() and end() functions:

require_once(”Cache/Output.php”);

$cache = new Cache_Output(”file”, array(”cache_dir” =&GT “cache/”) );

if ($contents = $cache-&GTstart(md5(”this is a unique key!”))) {

#
# aha, cached data returned
#

print $contents;
print “&LTp&GTCache Hit&LT/p&GT”;

} else {

#
# no cached data, or cache expired
#

print “&LTp&GTDon’t leave home without it…&LT/p&GT”; # place in cache
print “&LTp&GTStand and deliver&LT/p&GT”; # place in cache
print $cache-&GTend(10);

}

The Cache constructor takes the storage driver to use as the first parameter. File, database and shared memory storage drivers are available; see the pear/Cache/Container directory. Benchmarks by Ulf Wendel suggest that the “file” storage driver offers the best performance. The second parameter is the storage driver options. The options are “cache_dir”, the location of the caching directory, and “filename_prefix”, which is the prefix to use for all cached files. Strangely enough, cache expiry times are not set in the options parameter.

To cache some data, you generate a unique id for the cached data using a key. In the above example, we used md5(”this is a unique key!”).

The start() function uses the key to find a cached copy of the contents. If the contents are not cached, an empty string is returned by start(), and all future echo() and print() statements will be buffered in the output cache, until end() is called.

The end() function returns the contents of the buffer, and ends output buffering. The end() function takes as its first parameter the expiry time of the cache. This parameter can be the seconds to cache the data, or a Unix integer timestamp giving the date and time to expire the data, or zero to default to 24 hours.

Another way to use the PEAR cache is to store variables or other data. To do so, you can use the base Cache class:

&LT?php

require_once(”Cache.php”);

$cache = new Cache(”file”, array(”cache_dir” =&GT “cache/”) );
$id = $cache-&GTgenerateID(”this is a unique key”);

if ($data = $cache-&GTget($id)) {

print “Cache hit.&LTbr&GTData: $data”;

} else {

$data = “The quality of mercy is not strained…”;
$cache-&GTsave($id, $data, $expires = 60);
print “Cache miss.&LTbr&GT”;

}

?&GT

To save the data we use save(). If your unique key is already a legal file name, you can bypass the generateID() step. Objects and arrays can be saved because save() will serialize the data for you. The last parameter controls when the data expires; this can be the seconds to cache the data, or a Unix integer timestamp giving the date and time to expire the data, or zero to use the default of 24 hours. To retrieve the cached data we use get().

You can delete a cached data item using $cache-&GTdelete($id) and remove all cached items using $cache-&GTflush().

New: A faster Caching class is Cache-Lite. Highly recommended.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
1 comment