Sunday, April 02, 2006

How to cache smarty templates

By Justin Silverton

The Following is taken from the smarty manual:

What is caching?

Caching is used to speed up a call to display() or fetch() by saving its output to a file. If a cached version of the call is available, that is displayed instead of regenerating the output. Caching can speed things up tremendously, especially templates with longer computation times. Since the output of display() or fetch() is cached, one cache file could conceivably be made up of several template files, config files, etc.

Since templates are dynamic, it is important to be careful what you are caching and for how long. For instance, if you are displaying the front page of your website that does not change its content very often, it might work well to cache this page for an hour or more. On the other hand, if you are displaying a page with a weather map containing new information by the minute, it would not make sense to cache this page.

Setting it up

The first thing to do is enable caching by setting $caching = 1 (or 2).

Example: setting up caching

caching = true;


With caching enabled, the function call to display('index.tpl') will render the template as usual, but also saves a copy of its output to a file (a cached copy) in the $cache_dir. Upon the next call to display('index.tpl'), the cached copy will be used instead of rendering the template again.

Technical Note: The files in the $cache_dir are named similar to the template name. Although they end in the ".php" extention, they are not really executable php scripts. Do not edit these files!

Each cached page has a limited lifetime determined by $cache_lifetime. The default value is 3600 seconds, or 1 hour. After that time expires, the cache is regenerated. It is possible to give individual caches their own expiration time by setting $caching = 2. See $cache_lifetime for more details.

example: setting up cache lifetime

caching = 2; // lifetime is per cache

// set the cache_lifetime for index.tpl to 5 minutes
$smarty->cache_lifetime = 300;

// set the cache_lifetime for home.tpl to 1 hour
$smarty->cache_lifetime = 3600;

// NOTE: the following $cache_lifetime setting will not work when $caching = 2.
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// The home.tpl cache will still expire after 1 hour.
$smarty->cache_lifetime = 30; // 30 seconds

Caching Tips

Smarty caching can improve performance tremendously, but it doesn't always work the way that you think it should. In the following situation (which I have seen recently):

example: within (main.tpl) (this is a file named main.tpl)
{if some action}
{include file="menu1.tpl"}
{elseif some other action}
{include file="menu2.tpl"}

If the main template file (main.tpl) is cached, the includes template files will not get called properly. My solution is to separate the main file into cached and non-cached parts:

(within the PHP file that calls your smarty templates)

//do this on any separate template file that you want cached
$smarty->caching = true;
//the fetch command puts the output of a template file into a variable rather than //the screen
$menu1 = $smarty->fetch('menu1.tpl');
$menu2 = $smarty->fetch('menu2.tpl');

//set the main caching file to false
$smarty->caching = false;

(within the new main.tpl)
{if some action}
{elseif some other action}


Post a Comment

Links to this post:

Create a Link

<< Home