smarty缓存问题

 smarty中缓存cache的用法.
一、使用缓存
开启smarty缓存,只需将caching设为true,并指定cache_dir即可.
使用cache_lefetime指定缓存生存时间,单位为秒
要对相同页面生成多个不同的缓存,在display或fetch中加入第二参数cache_id,如$smarty->display('index.tpl',$my_cache_id);此特性可用于对不同的$_GET进行不同的缓存
二、清除缓存
clear_all_cache();//清除所有缓存
clear_cache('index.tpl');//清除index.tpl的缓存
clear_cache('index.tpl',cache_id);//清除指定id的缓存
三、使用自定义缓存方式
设置cache_handler_func使用自定义的函数处理缓存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
该函数的一般是根椐$action来判断缓存当前操作:
switch($action){
case "read"://读取缓存内容
case "write"://写入缓存
case "clear"://清空
}
一般使用md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id
如果需要,可使用gzcompress和gzuncompress来压缩和解压
四、局部关闭缓存
要在某些区域使缓存失效(只对需要的缓存),有几种方法:
insert:
定义一个insert标签要使用的处理函数,函数名格式为: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定义同上
register_block:
定义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名
注册block:$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存
模板写法:{name}内容{/name}
写成block插件:
1)定义一件插件函数:block.cacheless.php,放在smarty的plugins目录
block.cacheless.php的内容如下:
<?php
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
2) 编写程序及模板
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中的缓存

1、        一个页面一个缓存

2、        一个页面多个缓存(分页)

3、        局部关闭缓存(实时性)

缓存的目的:

PHP文件:解决功能上的问题(M+C)

          提供数据

HTML文件:界面效果(V)

缓存的配置步骤:

1)   配置文件中开启缓存

$tpl->caching=2

缓存时间(秒)

缓存目录

     2)$tpl->display(“index.tpl”);

   相当于把(编译后的文件产生的)静态页面只保留一份;

      $tpl->display(“index.tpl”,名称);

   相当于把(编译后的文件产生的)静态页面根据名称保留多份;

       例如:$tpl->display(“index.tpl”,1);

            $tpl->display(“index.tpl”,$_GET[‘page’]);

 $tpl->display(“index.tpl”,$_SERVER[‘REQUSET_URI’]);

3)        登陆用户名、时间日期等实时性要求比较高需要关闭缓存

l      <{insert }> 关闭缓存

*.php

 Function  insert_XXX(){}

 *.tpl

 <{insert name=XXX assign=XXX}>

l      写插件

./plugins/block.XXX.php

  Function smary_block_XXX(){}

编辑Smarty_compiler.class.php文件

 712行

 true---false

  if($tag_command==XXX){。。。。。。false}

   else{ 。。。。。。。。。。。。。。。。。。。。。。。true}

l      自定义函数

*.php

Function   XXXX(){}

$tpl->register_block(“”,””,false)

 

模板文件

  <{XXX}>  <{$DATE}>  <{XXX}>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值