#native_company# #native_desc#

Extending the PHPLIB Template class for caching

By Tom Anderson
on November 1, 2000

I’ve found the PHPLIB template class to be very useful for seperating code from logic and I thought it would be the perfect place for caching of dynamicly generated pages. The function I add to the class extends it so only one new line of code is required anywhere you want to cache a page and current functionality is not affected if no page caching is desired.

The limitation to caching is any page that uses the template must use the pparse() function to output the data. I’m sure other ways could be utilized, but I feel this implementation saves disk access and memory.

The extension is applied in three parts, instance variables, the set_cache() function, and a change in pparse().

To begin, the following instance variables are necessary:

/* for caching pages */
var $refreshCache = false;
var $cacheName = "";
var $cachePath = "/tmp/";
/* default = 15 min */
var $cacheTimeout = 900;

$cacheName should never be set as this will be the cached file name stored on the server. The $cachePath and $cacheTimeout are instance variables so defaults can be set. The $refreshCache boolean is the flag for whether or not to use the cached page or to run the script traditionally and save the cache.

If $refreshCache is false, the cached page will be used in set_cache() and the script will terminate. It will only be set to true in set_cache and if true, the old file will be deleted and the new data will be written later on in pparse().

Next, the one line addition to pparse():

function pparse($target, $handle, $append = false) {
$str = $this->parse($target, $handle, $append);

// For caching
if ($this->refreshCache) error_log($str, 3, $this->cachePath . $this->cacheName);

print $str;
return false;

The pparse function now writes to the cache then prints $str rather than just printing $str and exiting.

Finally, the new function, set_cache():

* set_cache ( array or string )
* Parameter: array or cache file name
* array("name" => "cache file name",
* "path" => "cache file path",
* "timeout" => seconds before refresh);
* or set_cache("cache file name");

function set_cache($cacheParms) {
if (is_array($cacheParms)) {
// Set name and the path to the cached file
$this->cacheName = $cacheParms["name"];
if ($cacheParms["path"]) $this->cachePath = $cacheParms["path"];
// Set timeout to 15 min. if not specified
if ($cacheParms["timeout"]) $this->cacheTimeout = $cacheParms["timeout"];
} else {
$this->cacheName = $cacheParms;

if (!$this->cacheName) return false;

// Get the last modified time for the file
## lstat[9] = FILE_LASTUPDATE
$fileProps = lstat($this->cachePath . $this->cacheName);

// Set the refresh cache to true if our delta is too big
if ($this -> refreshCache = ((time() - $fileProps[9]) > $this->cacheTimeout)) {
// Delete the cached file
@unlink($this->cachePath . $this->cacheName);
} else {
// Use the cache
include ($this->cachePath . $this->cacheName);

The comments for this function detail its use.