class.php");
$smarty = new Smarty;
$smarty->caching = 1;
$smarty->display("news.tpl");
?>
498 CHAPTER 19 ?– T EMPLAT I NG WITH SMARTY
Once enabled, calls to the display() and fetch()methods save the target
template??™s contents in the template specified by the $cache_dir attribute.
Working with the Cache Lifetime
Cached pages remain valid for a lifetime (in seconds) specified by the $cache_
lifetime attribute, which has a default setting of 3,600 seconds, or 1 hour. Therefore,
if you want to modify this setting, you could do it like so:
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->caching = 1;
// Set the cache lifetime to 30 minutes.
$smarty->cache_lifetime = 1800;
$smarty->display("news.tpl");
?>
Any templates subsequently called and cached during the lifetime of this object
would assume that lifetime.
It??™s also useful to override previously set cache lifetimes, allowing you to control
cache lifetimes on a per-template basis. You can do so by setting the $caching attribute to
2, like so:
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->caching = 2;
// Set the cache lifetime to 20 minutes.
$smarty->cache_lifetime = 1200;
$smarty->display("news.tpl");
?>
In this case, the news.tpl template??™s age will be set to 20 minutes, overriding whatever
global lifetime value was previously set.
CHAPTER 19 ?– TEMPLAT ING WITH SMARTY 499
Eliminating Processing Overhead with is_cached()
As mentioned earlier in this chapter, caching a template also eliminates processing
overhead that is otherwise always incurred when caching is disabled (leaving only
compilation enabled).
Pages:
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580