【smarty】smarty模板引擎缓存机制

首先说下smarty缓存和编译,这是两个不同的概念,编译默认情况下是启动的,而缓存机制需要人为开启,smarty编译过的文件还是php文件,所以执行的时候还是编译的,如果涉及到数据库,还是要访问数据库的所以开销也不小啦,所以需要smarty缓存来解决!

1.开启全局缓存

$smarty->cache_dir = "/caches/";  //缓存目录 
$smarty->caching = true;  //开启缓存,为flase的时侯缓存无效 
$smarty->cache_lifetime = 3600;  //缓存时间 

 

2.一个页面使用多个缓存

如:一个文章模板页面会生成多个文章页面,当然是缓存成很多页面,实现起来很简单,只要在display()方法设置第二个参数,指定唯一标识符即可。如下php代码:

$smarty->display('index.tpl',$_GET["article_id"]);

如上,通过第二个参数文章的id缓存一个文章页面。

 

3.为缓存减小开销

也就是说,已经缓存的页面无需进行数据库的操作处理了,可通过is_cached()方法判断!

if(!$smarty->is_cached('index.tpl')){

    //调用数据库

}

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

 

4.清除缓存

一般在开发过程中是不开启缓存的,因为在缓存时间内输出结果不变,但是在应用过程中开启缓存能大大提高web性能,清除缓存方法如下:

clear_all_cache();//清除所有缓存 
clear_cache('index.tpl');//清除index.tpl的缓存 
clear_cache('index.tpl',cache_id);//清除指定id的缓存 

 

5.关闭局部缓存

如果一个页面中一部分缓存,而另一部分不需要缓存,就可以这样做,比如说显示用户登录的名称就需要关闭缓存,smarty提供了如下三种解决方法:

(1)使用insert模板的一部分不被缓存

定义一个inser标签要使用的处理函数,函数名格式为:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是说,如果你定义的函数为insert_abc,则模板中使用方法为{insert name=abc}

参数通过$params传入

也可以做成insert插件,文件名命名为:insert.xx.php,函数命名为:smarty_insert_aa($params,&$smarty),xx定义同上

(2)$smarty->register_block($params, &$smarty)使整篇页面中的某一块不被缓存

定义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名

注册block:$smarty->register_block(name, smarty_block_name, false); //第三参数false表示该区域不被缓存

模板写法:{name}内容 {/name}

写成block插件:

第一步:定义一件插件函数:block.cacheless.php,放在smarty的 plugins目录

block.cacheless.php的内容如下:

<?php

function smarty_block_cacheless($param, $content, &$smarty) {

return $content;

}

?>

第二部:编写程序及模板

示例程序:testCacheLess.php

<?php

include(Smarty.class.php);

$smarty = new Smarty;

$smarty->caching=true;

$smarty->cache_lifetime = 6;

$smarty->display(cache.tpl);

?>

所用的模板:cache.tpl

已经缓存的:{$smarty.now}<br>

{cacheless}

没有缓存的:{$smarty.now}

{/cacheless}

现在运行一下,发现是不起作用的,两行内容都被缓存了

第三步:改写Smarty_Compiler.class.php(注:该文件很重要,请先备份,以在必要时恢复)

查找$this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true); 

修改成:

if($tag_command == cacheless) $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, false);

else $this->_plugins[block][$tag_command] = array($plugin_func, null, null, null, true);

你也可以直接将原句的最后一个参数改成false,即关闭默认缓存。

(3)使用register_function阻止插件从缓存中输出

index.tpl:
<div>{current_time}{/div}

index.php:
function smarty_function_current_time($params, &$smarty){
         return date("Y-m-d H:m:s");
}

$smarty=new smarty();
$smarty->caching = true;
$smarty->register_function('current_time','smarty_function_current_time',false);
if(!$smarty->is_cached()){
         .......
}
$smarty->display('index.tpl');

注解:
定义一个函数,函数名格式为:smarty_type_name($params, &$smarty)
type为function
name为用户自定义标签名称,在这里是{current_time}
两个参数是必须的,即使在函数中没有使用也要写上。两个参数的功能同上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值