smarty缓存问题

来源:互联网 发布:软件研发中心简介 编辑:程序博客网 时间:2024/05/22 02:29
        昨天,我给大家介绍了smarty的内建函数,相信大家也对smarty的内建函数有所了解了。今天,我给大家介绍一下smarty的缓存的应用。

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

一旦模板是动态〔应该不难理解〕的,哪些文档你加了缓存,缓存时间多长都是很重要的。举个例子,比如你站点的首页内容不是经常更改,那么对首页缓存一个小时或是更长都可得到很好效果。相反,几分钟就要更新一下信息的天气地图页面,用缓存就不好了。
Setting Up Caching [建立缓存]
首先要做的就是让缓存可用。这就要设置$caching = true(或 1.)
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;$smarty->display('index.tpl');
建立缓存后,display('index.tpl')函数会把模板返回原来状态〔没缓存〕,也会把输出保存copy〖n.名词〗到$cache_dir.下次调用display('index.tpl'),保存的缓存copy〖n.〗会被再用来代替原来的模板。
技术提示:在$chche_dir目录里的文档命名跟模板一致。尽管是用.php作为扩展名,但并不会被当作php代码来解析。所以不要去修改它。
每个缓存页都有一个用$cache_lifetime来控制的会话期。初始值是3600秒,就是一小时〔废话嘛〕。会话期结束,缓存就会重建。你可以通过设置$caching=2来控制单个缓存文件各自的的过期时间。祥细内容察看$cache_lifetime里面的列表。
如果$compile_check可用,每个跟缓存文档相关的模板文档和配置文档都会被检查来确定是否需要修改。在缓存产生后,改动任何文档,缓存也跟着更新改动。设置$compile_check为false,这是实现最佳性能的最小改动〔应该是这样:D〕。
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;$smarty->compile_check = true;$smarty->display('index.tpl');
一旦$force_compile可用,缓存文档会一直重建。这有效地关闭缓存。$force_compile只是用来调试,更有效关闭缓存的方法是让$caching = false(或0.)
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');
你可以插入模板函数insert来使部分页面动态化。除了在右下方显示的标语外整个页面都可以缓存。在缓存内容里面可以插入函数来使标语也动态化。查看相关文档关于insert的细节和例子。
 
你可以用clear_all_cache()来清除所有缓存,或用clear_cache()来清除单个缓存文档。
 
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');
 
Multiple Caches Per Page 每页多个缓存
你可以用单个函数display()或fetch()来输出多个缓存文档。display('index.tpl')在多种条件下会有不同的输出内容,要单独的把缓存分开。可以通过函数的第二参数cache_id来达到效果。
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;$my_cache_id = $_GET['article_id'];$smarty->display('index.tpl',$my_cache_id);
上面,我们通过变量$my_cache_id作为cache_id来display()。在index.tpl里$my_cache_id的每个唯一值,会建立单独的缓存。在这个例子里,"article_id"在URL传送,并用作cache_id。
技术提示:要注意从客户端(web浏览器)传值到Smarty(或任何PHP应用程序)的过程。尽管上面的例子用article_id从URL传值看起来很方便,却可能有糟糕的后果[安全问题]。cache_id被用来在文件系统里创建目录,如果用户想为article_id赋一个很大的值,或写一些代码来快速发送随机的article_ids,就有可能会使服务器出现问题。确定在使用它之前清空已存在的数据。在这个例子,可能你知道article_id的长度(值吧?!)是10字符,并只由字符-数字组成,在数据库里是个可用的article_id。Check for this!要注意检查这个问题!〔要注意这个提示!不用再说了吧?〕
确定传给is_cached()和clear_cache()的第二参数是同一个cache_id。
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;$my_cache_id = $_GET['article_id'];if(!$smarty->is_cached('index.tpl',$my_cache_id)) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty->assign($contents);}$smarty->display('index.tpl',$my_cache_id);
 
你可以通过把clear_cache()的第一参数设为null来为特定的cache_id清除所有缓存
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;// clear all caches with "sports" as the cache_id$smarty->clear_cache(null,"sports");$smarty->display('index.tpl',"sports");
通过这种方式,你可以用相同的cache_id来把你的缓存集合起来。
 
Cache Groups [缓存集合]
你可以通过建立cache_id集合做更祥细的集合体。在cache_id的值里用竖线"|"来分开子集合。你可以尽可能多的包含子集合。
require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;// clear all caches with "sports|basketball" as the first two cache_id groups$smarty->clear_cache(null,"sports|basketball");// clear all caches with "sports" as the first cache_id group. This would// include "sports|basketball", or "sports|(anything)|(anything)|(anything)|..."$smarty->clear_cache(null,"sports");$smarty->display('index.tpl',"sports|basketball");
技术提示:缓存集合并不像cache_id一样对模板使用路径。比如,如果你display('themes/blue/index.tpl'),那么在"themes/blue"目录下你并不能清除缓存。想要清除缓存,必须先用cache_id把缓存集合,像这样display('themes/blue/index.tpl','themes|blue');然后就可以用clear_cache(null,'themes|blue')清除blue theme(蓝色主题?!老外也真会叫...)下的缓存。
 
Controlling Cacheability of Plugins' Output
控制插件输出的缓冲能力
自从Smarty-2.6.0插件以来,如果注册它们,则插件的缓存能力能够被重新声明的。register_block,register_compiler_function 和register_function的第3个参数就是$ cacheable , 并且它的值默认为true。当然,在2.6.0版本之前它的默认值也是这样的。

当用$cacheable=false来这册一个插件,则每次这个页面被输出的时候,这个插件就会被使用,即使这个页面来自缓存。这个插件函数的行为有点像这个函数insert。

和{insert}相反,插件的属性默认是不缓存的。通过使用第四个参数 $cache_attrs ,它们能够被重新声明为缓存的。 $cache_attrs 是一个属性名字的数组,可以被缓存,所以每次当它被从缓存中取出的时候,这个插件函数获得值-----因为这个页面会被写入用来缓存。

阻止插件从缓存中输出

index.php:require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;function remaining_seconds($params, &$smarty) { $remain = $params['endtime'] - time(); if ($remain >=0) return $remain . " second(s)"; else return "done";}$smarty->register_function('remaining', 'remaining_seconds', false, array('endtime'));if (!$smarty->is_cached('index.tpl')) { // fetch $obj from db and assign... $smarty->assign_by_ref('obj', $obj);}$smarty->display('index.tpl');index.tpl:Time Remaining: {remain endtime=$obj->endtime}

直到$obj运行结束,时间的秒数在每一个页面输出的时候会改变,即使这个页面被缓存。只要结束时间属性被缓存,当页面被写入到缓存但是没有对这个页面接着的请求的时候对象只是不得不重新从数据库里取出而已。

阻止一个模板文件的 整篇被缓存

index.php:require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;function smarty_block_dynamic($param, $content, &$smarty) { return $content;}$smarty->register_block('dynamic', 'smarty_block_dynamic', false);$smarty->display('index.tpl');index.tpl:Page created: {"0"|date_format:"%D %H:%M:%S"}{dynamic}Now is: {"0"|date_format:"%D %H:%M:%S"}... do other stuff ...{/dynamic}

当重新加载这个页面,你将会注意到这两个日期不同。一个是“动态“,一个是“静态”。你能够在{dynamic}...{/dynamic}之间作任何事情,并且保证它将不会像剩下的页面一样被缓存。

原创粉丝点击