<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"><?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;$smarty->display('index.tpl');?></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->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"><?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty->caching = Smarty::CACHING_LIFETIME_SAVED; // lifetime is per cache
// set the cache_lifetime for index.tpl to 5 minutes$smarty->cache_lifetime = 300;$smarty->display('index.tpl');
// set the cache_lifetime for home.tpl to 1 hour$smarty->cache_lifetime = 3600;$smarty->display('home.tpl');
// 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->cache_lifetime = 30; // 30 seconds$smarty->display('home.tpl');?></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"><?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;$smarty->compile_check = true;$smarty->display('index.tpl');?></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"><?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;if(!$smarty->is_cached('index.tpl')) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty->assign($contents);}$smarty->display('index.tpl');?></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"><?phprequire('Smarty.class.php');$smarty = new Smarty;$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;// clear only cache for index.tpl$smarty->clear_cache('index.tpl');// clear out all cache files$smarty->clear_all_cache();$smarty->display('index.tpl');?></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>
- Smarty模板编译引擎
- 序
- 译序
- I.开始
- 第一章. 什么是Smarty?
- 第二章. 安装
- II.模板设计者篇
- 第三章.基本语法
- 注释
- 变量
- 函数
- 属性
- 双引号里嵌入变量
- 数学运算
- 忽略Smarty解析
- 第四章.变量
- 从PHP分配的变量
- 从配置文件读取的变量
- 变量范围
- {$smarty}保留变量
- 第五章.变量调节器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.组合修改器
- 第七章.内置函数
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定义函数
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.调试控制台
- III.模板程序员篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty类变量
- $template_dir
- 第十三章.Smarty类方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.缓存
- 建立缓存
- 多重缓存
- 缓存集合
- 控制插件输出的可缓存性
- 第十五章.高级特性
- 安全
- 通过模板更改设置
- 模板继承
- 数据流
- 对象
- 静态类
- 预过滤器
- 后过滤器
- 输出过滤器
- 缓存处理函数
- 资源
- 第十六章.以插件扩展Smarty
- 插件如何工作
- 命名约定
- 编写插件
- 模板函数
- 调节器
- 块函数
- 编译函数
- 预滤器/后滤器
- 输出过滤器
- 资源
- 插入
- Ⅳ.附录
- 第十七章.疑难解答
- Smarty/PHP 错误
- 第十八章.使用技巧和经验
- 空白变量处理
- 默认变量处理
- 传递变量标题给头模板
- 日期
- WAP/WML
- 组件化模板
- 拒绝电子邮件地址
- 第十九章. 相关资源
- 第二十章. 漏洞
- 3.0安装包
- 2.x版本升级至3.x版本的提示
- 3.0.x使用指南
- 翻译人员列表