directory search
Smarty模板编译引擎 I.开始 第一章. 什么是Smarty? 第二章. 安装 要求 基本安装 扩展设置 II.模板设计者篇 第三章.基本语法 注释 函数 属性 第四章.变量 从PHP分配的变量 Associative arrays Array indexes Objects 从配置文件读取的变量 {$smarty}保留变量 Request variables {$smarty.now} {$smarty.const} {$smarty.capture} {$smarty.config} {$smarty.section} {$smarty.template} 第五章.变量调节器 capitalize count_characters cat 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 第六章.组合修改器 第七章.内建函数 capture config_load foreach include include_php insert if ldelim literal php section index index_prev index_next iteration first last rownum loop show total strip 第八章.自定义函数 assign counter cycle debug eval fetch html_checkboxes html_image html_options html_radios html_select_date html_select_time html_table math mailto popup_init popup textformat 第九章.配置文件 第十章.调试控制台 III.模板程序员篇 第十一章 常量 SMARTY_DIR 第十二章 变量 $template_dir $compile_dir $config_dir $plugins_dir $debugging $debug_tpl $debugging_ctrl $global_assign $undefined $autoload_filters $compile_check $force_compile $caching $cache_dir $cache_lifetime $cache_handler_func $cache_modified_check $config_overwrite $config_booleanize $config_read_hidden $config_fix_newlines $default_template_handler_func $php_handling $security $secure_dir $security_settings $trusted_dir $left_delimiter $right_delimiter $compiler_class $request_vars_order $compile_id $use_sub_dirs $default_modifiers $default_resource_type 第十三章.方法 append append_by_ref assign assign_by_ref clear_all_assign clear_all_cache clear_assign clear_cache clear_compiled_tpl clear_config config_load display fetch get_config_vars get_registered_object get_template_vars is_cached load_filter register_block register_compiler_function register_function register_modifier register_object register_outputfilter register_postfilter register_prefilter register_resource trigger_error template_exists unregister_block unregister_compiler_function unregister_function unregister_modifier unregister_object unregister_outputfilter unregister_postfilter unregister_prefilter unregister_resource 第十四章.缓存 Setting Up Caching Multiple Caches Per Page Cache Groups Controlling Cacheability of Plugins' Output 第十五章.高级特点 Objects Prefilters Postfilters Output Filters Cache Handler Function Resources Templates from $template_dir Templates from any directory Templates from other sources Default template handler function 第十六章.以插件扩展Smarty How Plugins Work Naming Conventions Writing Plugins Template Functions Modifiers Block Functions Compiler Functions Prefilters/Postfilters Output Filters Resources Inserts Ⅳ.高级特点 第十七章.疑难解答 Smarty/PHP errors 第18章.使用技巧和经验 Blank Variable Handling Default Variable Handling Passing variable title to header template Dates WAP/WML Componentized Templates Obfuscating E-mail Addresses 第十九章. 相关资源 第二十章. 漏洞
characters

Chapter 14. Caching [缓存]

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

Setting Up Caching [建立缓存]

The first thing to do is enable caching. This is done by setting $caching = true (or 1.)
首先要做的就是让缓存可用。这就要设置$caching = true(或 1.)

Example 14-1. enabling caching
例14-1.使缓存可用

require('Smarty.class.php'); $smarty = new Smarty; $smarty->caching = true; $smarty->display('index.tpl');

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. 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里面的列表。

Example 14-2. setting cache_lifetime per cache
例14-2 设置单个缓存会话期〔时间〕

require('Smarty.class.php'); $smarty = new Smarty; $smarty->caching = 2; // 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.
//提示:在$chching=2后面的$chche_lifetime不会起作用。
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// home.tpl的缓存会话期设为1小时后,不会再按$cache_lifetime的值改变。
// The home.tpl cache will still expire after 1 hour.
// home.tpl的缓存会在1小时后结束。$smarty->cache_lifetime = 30; // 30 seconds
$smarty->display('home.tpl');

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, leave $compile_check set to false.
如果$compile_check可用,每个跟缓存文档相关的模板文档和配置文档都会被检查来确定是否需要修改。在缓存产生后,改动任何文档,缓存也跟着更新改动。设置$compile_check为false,这是实现最佳性能的最小改动〔应该是这样:D〕。

Example 14-3. enabling $compile_check
例14-3.可用$compile_check

require('Smarty.class.php'); $smarty = new Smarty; $smarty->caching = true; $smarty->compile_check = true; $smarty->display('index.tpl');

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 = 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()函数可用来测试一个模板是否有有效的缓存。如果一个缓存模板需要从数据库中获取数据,可以用这个函数来跳过这个过程。

Example 14-4. using is_cached()
例14-4.使用is_cached()

require('Smarty.class.php'); $smarty = new Smarty; $smarty->caching = true; if(!$smarty->is_cached('index.tpl')) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty->assign($contents); } $smarty->display('index.tpl');

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 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');
Previous article: Next article: