If you use Cheetah, please support the project and register by clicking here:


7.4.1 Caching individual placeholders

By default, the values of each $placeholder is retrieved and interpolated for every request. However, it's possible to cache the values of individual placeholders if they don't change very often, in order to speed up the template filling.

To cache the value of a single $placeholder, add an asterisk after the $; e.g., $*var. The first time the template is filled, $var is looked up. Then whenever the template is filled again, the cached value is used instead of doing another lookup.

The $* format caches ``forever''; that is, as long as the template instance remains in memory. It's also possible to cache for a certain time period using the form $*<interval>*variable, where <interval> is the interval. The time interval can be specified in seconds (5s), minutes (15m), hours (3h), days (2d) or weeks (1.5w). The default is minutes.

<HTML>
<HEAD><TITLE>$title</TITLE></HEAD>
<BODY>

$var ${var}           ## dynamic - will be reinterpolated for each request
$*var2 $*{var2}       ## static - will be interpolated only once at start-up
$*5*var3 $*5*{var3}   ## timed refresh - will be updated every five minutes.

</BODY>
</HTML>

Note that ``every five minutes'' in the example really means every five minutes: the variable is looked up again when the time limit is reached, whether the template is being filled that frequently or not. Keep this in mind when setting refresh times for CPU-intensive or I/O intensive operations.

If you're using the long placeholder syntax, ${}, the braces go only around the placeholder name: $*.5h*{var.func('arg')}.

Sometimes it's preferable to explicitly invalidate a cached item whenever you say so rather than at certain time intervals. You can't do this with individual placeholders, but you can do it with cached regions, which will be described next.

If you use Cheetah, please support the project and register by clicking here: