Pressure is nothing more than the shadow of great opportunity. - Michael Johnson

Caching added to my template class

3 years, 2 months ago

I've just finished adding caching to my template class which I first posted details of in "PHP Tip: Extract, Variable Variables and Templating". Here's an example of implementing caching:

  1. <?php
  2. require('template-engine-v2.inc.php');
  3. $oTemplate = new Template('test.php');
  4. $oTemplate->AddCacheCondition('home');
  5. if (!$oTemplate->IsCached()) {
  6. $oTemplate->Set('title', 'Do links on your site make sense out of context?');
  7. $oTemplate->Set('author', 'Ed Eliot');
  8. $oTemplate->Set('content', new Template('content.php'));
  9. }
  10. echo $oTemplate->Display();
  11. ?>

The class now supports two additional methods:

  • SetCacheLength($iCacheLength) - this takes a single parameter which specifies the number of seconds you want the output cached for.
  • AddCacheCondition($vCondition) - this takes a string or array representing one or more data values which should be used to form the cache key. You might add the current post id, for example, as a condition to ensure individual caching of each post.

When sub-templates are directly assigned to the Set method of a parent template they inherit the parent's cache settings. If you want to specify caching properties for sub-templates individually you'll need to instead assign their output to the parent template.

Download version 2 of the template class. You'll also need to download a copy of my caching class. Alternatively you can view the source of the cache class.

Comments

  • Loving the templating class... smarty is definately overkill for smaller projects.

    Quick question - how would I clear the cache for a particular page? For instance if a cached post was updated?

    Mark Perkins - 7th July 2007 #

  • Mark - great to hear it's proving useful.

    Perhaps your easiest way to clear the cache is to add an additional condition to your code at the point at which it checks the validity of the cache. This can check for the presence of a query string parameter and force re-writing of the cache accordingly.

    if (!$oTemplate->IsCached() || isset($_GET['clear'])) {
    
    }

    If you're wanting to actually delete the cache file from disk that's slightly trickier. I've been meaning to add a remove method to the caching class itself for this situation so I guess now is the time to get on and code it.

    Ed Eliot - 7th July 2007 #

  • Thanks Ed - The query string method sounds like a good stop gap solution, but a proper cache clear would certainly make me happy! I look forward to seeing what you come up with in due course....

    Mark Perkins - 7th July 2007 #

  • Hi Ed,

    thanks for your efforts in building such a nice class for your idea. I'm a first-time user of it and found myself getting an error, when I try to use Set() with the automatic convert entities option. In my opinion you missed a parameter for the function call to htmlentities(). The quote_style is optional, yes. But you need to define it, if you want to use an option following, don't you? Well, I just wanted to point this out!

    Have a nice day,

    Jan

    Jan Almeroth - 15th September 2008 #

  • Hi Ed,

    thanks for your efforts in building such a nice class for your idea. I'm a first-time user of it and found myself getting an error, when I try to use Set() with the automatic convert entities option. In my opinion you missed a parameter for the function call to htmlentities(). The quote_style is optional, yes. But you need to define it, if you want to use an option following, don't you? Well, I just wanted to point this out!

    Have a nice day,

    Jan

    Jan Almeroth - 15th September 2008 #

Help make this post better

Notes: Standard BBCode for links, bold, italic and code are supported. rel="nofollow" is added to all links. Your email address will never be displayed on the site.

Back to index