> Smarty模板引擎中文在线手册 > 14. Caching [缓存]

Setting Up Caching [建立缓存]
Multiple Caches Per Page [每页多个缓存]
Cache Groups [缓存集合]
Controlling Cacheability of Plugins' Output [控制插件输出的缓冲能力]

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.
缓存被用来保存一个文档的输出从而加速display()或fetch()函数的执行。如果一个函数被加进缓存,那么实际输出的内容将用缓存来代替。缓存可让事物非常快速的执行,特别是带有长计算时间的模板。一旦display()或fetch()用缓存输出,那么一个缓存文档将非常容易用几个模板文档或是配置文档等来组成〔功力不小〕。

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.
一旦模板是动态〔应该不难理解〕的,哪些文档你加了缓存,缓存时间多长都是很重要的。举个例子,比如你站点的首页内容不是经常更改,那么对首页缓存一个小时或是更长都可得到很好效果。相反,几分钟就要更新一下信息的天气地图页面,用缓存就不好了。

$caching = true (or 1.)
首先要做的就是让缓存可用。这就要设置$caching = true(或 1.)

$cache_dir. Upon the next call to display('index.tpl'), the cached copy will be used instead of rendering the template again.
建立缓存后,display('index.tpl')函数会把模板返回原来状态〔没缓存〕,也会把输出保存copy〖n.名词〗到$cache_dir.下次调用display('index.tpl'),保存的缓存copy〖n.〗会被再用来代替原来的模板。

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!
技术提示:在$chche_dir目录里的文档命名跟模板一致。尽管是用.php作为扩展名,但并不会被当作php代码来解析。所以不要去修改它。

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 the documentation on $cache_lifetime for details.
每个缓存页都有一个用$cache_lifetime来控制的会话期。初始值是3600秒,就是一小时〔废话嘛〕。会话期结束,缓存就会重建。你可以通过设置$caching=2来控制单个缓存文件各自的的过期时间。祥细内容察看$cache_lifetime里面的列表。

$compile_check is enabled, every template file and config file that is involved with the cache file is checked for modification. If any of the files have been modified since the cache was generated, the cache is immediately regenerated. This is a slight overhead so for optimum performance, leave $compile_check set to false.
如果$compile_check可用,每个跟缓存文档相关的模板文档和配置文档都会被检查来确定是否需要修改。在缓存产生后,改动任何文档,缓存也跟着更新改动。设置$compile_check为false,这是实现最佳性能的最小改动〔应该是这样:D〕。

$force_compile is enabled, the cache files will always be regenerated. This effectively turns off caching. $force_compile is usually for debugging purposes only, a more efficient way of disabling caching is to set $caching = false (or 0.)
一旦$force_compile可用,缓存文档会一直重建。这有效地关闭缓存。$force_compile只是用来调试,更有效关闭缓存的方法是让$caching = false(或0.)

The is_cached() function can be used to test if a template has a valid cache or not. If you have a cached template that requires something like a database fetch, you can use this to skip that process.
is_cached()函数可用来测试一个模板是否有有效的缓存。如果一个缓存模板需要从数据库中获取数据,可以用这个函数来跳过这个过程。

insert template function. Let's say the whole page can be cached except for a banner that is displayed down the right side of the page. By using an insert function for the banner, you can keep this element dynamic within the cached content. See the documentation on insert for details and examples.
你可以插入模板函数insert来使部分页面动态化。除了在右下方显示的标语外整个页面都可以缓存。在缓存内容里面可以插入函数来使标语也动态化。查看相关文档关于insert的细节和例子。

You can clear all the cache files with the clear_all_cache() function, or individual cache files (or groups) with the clear_cache() function.
你可以用clear_all_cache()来清除所有缓存,或用clear_cache()来清除单个缓存文档。

Example 14-5. clearing the cache
例14-5.清除缓存

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;

// clear out all cache files
$smarty->clear_all_cache();

// clear only cache for index.tpl
$smarty->clear_cache('index.tpl');

$smarty->display('index.tpl');