Article

Beyond The Template Engine

Page: 1 2 3 4 5 Next

Caching

With a solution as simple as this, implementing template caching becomes a fairly simple task. For caching, we have a second class, which extends the original template class. The CachedTemplate class uses virtually the same API as the original template class. The differences are that we must pass the cache settings to the constructor and call fetch_cache() instead of fetch().

The concept of caching is simple. Basically, we set a cache timeout that represents the period (in seconds) after which the output should be saved. Before doing all the work to generate a page, we must first test to see if that page has been cached, and whether the cache is still considered current. If the cache is there, we don't need to bother with all the database work and business logic it takes to generate the page -- we can simply output the previously cached content.

This practice presents the problem of being able to uniquely identify a cache file. If a site is controlled by a central script that displays output based on GET variables, having only one cache for every PHP file will be unsuccessful. For instance, if you have index.php?page=about_us, the output should be completely different than that which would be returned if a user called index.php?page=contact_us.

This problem is solved by generating a unique cache_id for each page. To do this, we take the actual file that was requested and hash it together with the REQUEST_URI (basically, the entire URL: index.php?foo=bar&bar=foo). Of course, the hashing is taken care of by the CachedTemplate class, but the important thing to remember is that you absolutely must pass a unique cache_id when you create a CachedTemplate object. Examples follow, of course.

Use of a caching set up involves these steps.

  1. include() the Template source file

  2. create a new CachedTemplate object (and pass the path to the templates, the unique cache_id, and the cache time out)

  3. test to see if the content is already cached

  4. if so, display the file and script execution ends

  5. otherwise, do all the processing and fetch() the template

  6. the fetch_cache() call will automatically generate a new cache file

This script assumes your cache files will go in ./cache/, so you must create that directory and chmod it so the Web server can write to it. Also note that if you find an error during the development of any script, the error can be cached! Thus it’s a good idea to disable caching altogether while you’re developing. The best way to do this is to pass zero (0) as the cache lifetime -- that way, the cache will always expire immediately.

Here’s an example of caching in action.

<?php  
/**  
* Example of cached template usage.  Doesn't provide any speed increase since  
* we're not getting information from multiple files or a database, but it  
* introduces how the is_cached() method works.  
*/  
 
/**  
* First, include the template class.  
*/  
require_once('template.php');  
 
/**  
* Here is the path to the templates.  
*/  
$path = './templates/';  
 
/**  
* Define the template file we will be using for this page.  
*/  
$file = 'list.tpl.php';  
 
/**  
* Pass a unique string for the template we want to cache.  The template  
* file name + the server REQUEST_URI is a good choice because:  
*    1. If you pass just the file name, re-used templates will all  
*       get the same cache.  This is not the desired behavior.  
*    2. If you just pass the REQUEST_URI, and if you are using multiple  
*       templates per page, the templates, even though they are completely  
*       different, will share a cache file (the cache file names are based  
*       on the passed-in cache_id.  
*/  
$cache_id = $file . $_SERVER['REQUEST_URI'];  
$tpl = & new CachedTemplate($path, $cache_id, 900);  
 
/**  
* Test to see if the template has been cached.  If it has, we don't  
* need to do any processing.  Thus, if you put a lot of db calls in  
* here (or file reads, or anything processor/disk/db intensive), you  
* will significantly cut the amount of time it takes for a page to  
* process.  
*  
* This should be read aloud as "If NOT Is_Cached"  
*/  
if(!($tpl->is_cached())) {  
   $tpl->set('title', 'My Title');  
   $tpl->set('intro', 'The intro paragraph.');  
   $tpl->set('list', array('cat', 'dog', 'mouse'));  
}  
 
/**  
* Fetch the cached template.  It doesn't matter if is_cached() succeeds  
* or fails - fetch_cache() will fetch a cache if it exists, but if not,  
* it will parse and return the template as usual (and make a cache for  
* next time).  
*/  
echo $tpl->fetch_cache($file);  
?>

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links