php smarty 高级缓存技巧方法


cache是smarty区别于其他模板系统的一个很重要的地方:

  1. 设置当前这个页面的缓存时间

  2. 设置当前这个页面的CacheID,模板编号可以使用“|”分组。

  3. 判断当前页面有没有被缓存过,如果缓存过,则直接读取缓存文件,没有缓存或者缓存过期,则执行正常的逻辑查找数据,显示页面,同时生成缓存文件。

1.配置cache
include("Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="./templates";
$smarty->compile_dir="./templates_c";
$smarty->config_dir   ="./config";
$smarty->caching=true;

2.使用cache
如果模板为index.html;则
if($smarty->is_cached("index.html"))
{
   $smarty->display("index.html");
   exit;
}
也可以用
$smarty->force_compile   =   false; (配置cache时)
来实现。
3.使用cache_id
$a=$_GET["a"];   
if   ($a=="about")   {   
      $cache_id="about";$title="公司介绍";   
}   
else   {   
      $cache_id="contact";$title="联系我们";   
}   
    
$smarty->assign('article',$article);   
$smarty->caching   =   true; //缓存页面   
$smarty->display('basic.tpl',$cache_id);   
    如果我这个模板basic.tpl的缓存ID有很多个呢?比如news.php?newsid=xxx,   缓存ID为newsid,如何进行全部清除呢?   
$smarty->clear_cache("basic.tpl")   //试过是不行的   
    
$smarty->clear_all_cache();   //显然会把不想清的别的模板的缓存也给清空了。   
现在我在后台对这个带参数的页面basic.php?a=about   或=contact   
里所涉及到的东东进行修改更新数据库操作。当然这时候也要清空一下这个basic.tpl模板的所有缓存即缓存号为about和contact的两个缓存。   
    
//   清除某一模板的多个缓存中指定缓存号的一个   
$smarty->clear_cache("basic.tpl","about");   
$smarty->clear_cache("basic.tpl","contact");  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值