samrty解决局部不缓存的三种办法

本文探讨了在需求实时性较高的场景中,如何通过局部不缓存策略来优化页面显示效率。介绍了三种实现方法:通过自定义插件、在PHP文件中注册块函数、使用Smarty内置函数insert,每种方法都详细阐述了实现步骤和关键代码片段,旨在提高用户体验和系统响应速度。

当实时性要求比较高的时候(例如登录:欢迎。。。。登录)

   解决的办法:局部不缓存;

1、第一种方法:

1)、指定该函数的名称:nocache

2)、在插件目录下(plugins)创建一个块  命名为:block.nocache.php

      该函数中的内容:

<?php

       function smarty_block_nocache($args,$content){

              return$content;

}

?>

 

*.Php

$tpl->assign(“data”.date(“H:i:s”));

 

*.tpl

<{nocache}><{$data}><{/nocache}>

 

写成插件时,要配置Smarty_Compiler.class.php  让函数为:nocache的当执行它的时候不被缓存; 操作如下:文件名:  Smarty_Compiler.class.php 

行数:  713---719 

针对smarty版本:   3.1.4

       

       if($tag_command==nocache){

                   $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);

 

            }

 

 

第二种:写在php文件中:

 

 

*.php

$tpl->register_block("nocache","fun1",false);  false代表当执行该块时是不被缓存的。

 

function fun1($args,$content){ return $content; }

 

$tpl->assign("time",date("H:i:s"));

 

*.tpl

<{nocache}><{$time}><{/nocache}>

 

 

 

第三种:smarty内建函数insert:

*.php

       function insert_fun1(){return date(“H:i:s”); }

       注意:用内建函数insert时,函数名都要加上insert_你要定义的函数名

*.tpl

       <insert name=”fun1”>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值