<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="caching.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom"/><td width="25%" align="right" valign="bottom"><a href="caching.multiple.caches.html" accesskey="N">Next</a></td></tr></table> Setting Up Caching [建立缓存] The first thing to do is enable caching by setting $caching = 1 (or 2). 首先启用缓存。设置$caching = 1(或 2) 。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td> <div class="EXAMPLE"> <a name="AEN3141" id="AEN3141"> </a> <b><span class="PROGRAMLISTING">Example 14.1. Enabling caching</span><br/> 例14-1.启动缓存</b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <pre class="PROGRAMLISTING">&lt;?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty-&gt;caching = Smarty::CACHING_LIFETIME_CURRENT;$smarty-&gt;display('index.tpl');?&gt;</pre> </td> </tr></table></div> </td> </tr></table> 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. On the next call to display('index.tpl'), the cached copy will be used instead of rendering the template again. 缓存启动后,当函数调用display('index.tpl')时会照常渲染模板,同时还会将其输出保存为一个缓存文件放到[$cache_dir](#)目录下。当下次调用display('index.tpl')时,程序将直接取出缓存文件,而不是再次渲染模板。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td><p>Technical Note<br/> The files in the $cache_dir are named similar to the template name. Although they end in the .php extention, they are not intended to be directly executable. Do not edit these files!</p> <p>在$chche_dir目录里的文件命名与模板一致。尽管使用.php作为扩展名,但并不会当作php代码执行。所以不要去修改它。</p></td> </tr></table> Each cached page has a limited lifetime determined by $cache_lifetime. The default value is 3600 seconds ie an 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. 每个缓存页面由[$cache_lifetime](#)变量控制文件的生存周期。初始值是3600秒,就是一小时〔废话嘛〕。周期结束,缓存就会重建。你可以通过设置$caching=2来控制单个缓存文件自己的生存周期。详情查看$cache_lifetime里面的列表。 <table width="80%" class="note"><caption> 译注 </caption> <tr><td>$smarty-&gt;caching设置为1、Smarty::CACHING_LIFETIME_CURRENT和Smarty::CACHING_LIFETIME_SAVED有什么不同?<br/> 设为1仅表示开启缓存,使用默认值$caching_time = 3600s,如果缓存周期到期,则不再使用缓存;而Smarty::CACHING_LIFETIME_CURRENT则会先判断是否过期,如果没有则继续使用当前缓存,如果过期则重新建立缓存,php脚本内设置的$caching_time对所有缓存有效;而Smarty::CACHING_LIFETIME_SAVED用以设置php脚本内的单个缓存,比如加载进来的模板缓存。</td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td> <div class="EXAMPLE"> <a name="AEN3142" id="AEN3142"> </a> <b><span class="PROGRAMLISTING">Example 14.2. Setting $cache_lifetime per cache</span><br/> 例14-2 设置单个缓存周期 </b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <pre class="PROGRAMLISTING">&lt;?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty-&gt;caching = Smarty::CACHING_LIFETIME_SAVED; // lifetime is per cache&#13; // set the cache_lifetime for index.tpl to 5 minutes$smarty-&gt;cache_lifetime = 300;$smarty-&gt;display('index.tpl');&#13; // set the cache_lifetime for home.tpl to 1 hour$smarty-&gt;cache_lifetime = 3600;$smarty-&gt;display('home.tpl');&#13; // 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.//注意:当设$caching=2,接下来的$cache_lifetime设置并不会影响home.tpl工作,因为home.tpl缓存周期在前面已经设为1小时,//其不再理会后面$cache_lifetime的值,home.tpl缓存周期仍为1小时。$smarty-&gt;cache_lifetime = 30; // 30 seconds$smarty-&gt;display('home.tpl');?&gt;</pre></td> </tr></table></div> </td> </tr></table> If $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, set $compile_check to FALSE. 如果启用[$compile_check](#),将会检查缓存文件的原模板、配置文件是否经过修改。在缓存产生后,如果原文件有任何改动,其缓存文件也立即随着更新。设置[$compile_check](#)为false,可以做到以最小的开销实现最佳的性能(应该是这样:D)。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td> <div class="EXAMPLE"> <a name="AEN3685"> </a> <b><span class="PROGRAMLISTING">Example 14.3. Enabling $compile_check</span><br/> 例14-3.启动$compile_check </b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <pre class="PROGRAMLISTING">&lt;?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty-&gt;caching = Smarty::CACHING_LIFETIME_CURRENT;$smarty-&gt;compile_check = true;$smarty-&gt;display('index.tpl');?&gt;</pre></td> </tr></table></div> </td> </tr></table> If $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](#) = 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. 如果启用[$force_compile](#),缓存文件会一直重建。这样做实际上等于关闭了缓存。$force_compile只是用来调试,一个更关闭缓存的更好方法是设$caching = 0。 [is_cached()](#)函数可用来测试一个模板是否建立了有效的缓存。如果缓存模板需要发送一些请求,如从数据库中获取数据,可以用这个函数来跳过再次请求的过程。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td> <div class="EXAMPLE"> <strong><a name="AEN31444" id="AEN31444"> </a>Example 14.4. Using is_cached()</strong><b><br/> 例14-4.使用is_cached() </b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <pre class="PROGRAMLISTING">&lt;?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty-&gt;caching = Smarty::CACHING_LIFETIME_CURRENT;if(!$smarty-&gt;is_cached('index.tpl')) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty-&gt;assign($contents);}$smarty-&gt;display('index.tpl');?&gt;</pre></td> </tr></table></div> </td> </tr></table> You can keep parts of a page dynamic with the {insert} template function. Let's say the whole page can be cached except for a banner that is displayed down the side of the page. By using the {insert} function for the banner, you can keep this element dynamic within the cached content. See the documentation on {insert} for more details and examples. You can clear all the cache files with the clear_all_cache() function, or individual cache files and groups with the clear_cache() function. 你可以插入模板函数[{insert}](#)来使部分页面动态化。假如除页脚的banner广告须动态显示外,其它页面都可以缓存。那么就用{insert}函数插入banner,当页面缓存后,它还是一个动态显示的元素,换句话说,内容缓存并未影响到banner广告的更新显示。详情查看[{insert}](#)相关文档。 你可以用[clear_all_cache()](#)来清除所有缓存,或用[clear_cache()](#)来清除单个缓存文件或缓存组。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td> <div class="EXAMPLE"> <a name="AEN3145" id="AEN3145"> </a> <b>Example 14-5. clearing the cache<br/> 例14-5.清除缓存 </b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <pre class="PROGRAMLISTING">&lt;?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty-&gt;caching = Smarty::CACHING_LIFETIME_CURRENT;// clear only cache for index.tpl$smarty-&gt;clear_cache('index.tpl');// clear out all cache files$smarty-&gt;clear_all_cache();$smarty-&gt;display('index.tpl');?&gt;</pre></td> </tr></table></div> </td> </tr></table> <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="caching.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="caching.multiple.caches.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Caching<br/> 缓存 </td> <td width="34%" align="center" valign="top"><a href="smarty.for.programmers.html" accesskey="U">Up</a></td> <td width="33%" align="right" valign="top">Multiple Caches Per Page<br/> 多重缓存 </td> </tr></table>