php smarty关闭缓存,PHP之三大缓存:smarty缓存、客户端缓存、PDO缓存之smarty缓存...

smarty缓存

a.基本smarty缓存

1)开启smarty缓存机制 smarty对象的$caching属性true或者1代表开启缓存,false或者0代表关闭缓存

$smarty->caching=true

2)设置缓存时间 smarty对象的cache_lifetime属性值为整型单位为秒

$smarty->cache_lifetime【单位为秒】

3)设置smarty编译前检查缓存是否需要更新 smarty对象的$compile_check属性true代表检查false代表不检查

$smarty->compile_check=true

注意:为了最大性能,确定将$compile_check设为”false”.注意:如果设为了”false”,虽然模板文件被修改,但你不会看到修改结果,因为模板没有得到重新编译

4)判断是否已经缓存了模板 samrty对象的boolean is_cached (string $template, [string $cache_id])方法

$smarty->is_cached(‘index.tpl’[,$cache_id])

注意:在指定模板的缓存存在是返回真

$template指定检查的模板文件名

$cache_id如果模板有多个缓存的话,可以通过其指定缓存号

5)清除缓存 smarty对象的void clear_all_cache(int $expire_time)方法

$smarty->clear_all_cache(),其中$expire_time是指超过其指定的时间的所有缓存都清除

6)清除指定模板缓存 smarty对象的void clear_cache(string $template [,string $cache_id[,string $compile_id [,int$expire_time]]])

$smarty->clear_cache(‘index.tpl’)

$smarty->clear_cache(‘index.tpl’,$caceh_id)

其中:$cache_id是指如果这个模板有多个缓存,你可以通过其指定要清除缓存的缓存号

$compile_id是指如果这个模板有多个缓存,你可以通过其指定要清除缓存编译号

$expire_time用来指定超过某一时间(以秒为单位)的缓存才会被清除

7)一个页面设置多个缓存可以向smarty对象的display方法传递第二个cache_id参数

$smarty->display(‘index.tpl’,$cache_id)

注意:$cache_id指定缓存的ID,如果已经缓存直接获取缓存ID所指定的缓存内容,否则建立相应缓存ID的缓存文件

保证传递给is_cache()和clear_cache()方法的cache_id为同一个缓存ID

可以通过将clear_cache()的第一个参数设置为null,然后指定第二个参数即$cache_id来清除指定cache_id的缓存文件

8)设置缓存目录 smarty对象的cache_dir属性,值可以为相对路径【相对运行的PHP脚本】也可以是绝对路径

$smarty->cache_dir=’../cache’

注意:默认的缓存目录为’./cache’,不推荐将缓存目录放在WEB服务器根目录下

b.smarty局部缓存机制

1)使用smarty的insert函数使局部不缓存

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

3)使用register_block使用整个页面中的一部分不缓存

补充说明:smarty的function、block、insert插件的用法

insert的用法

index.tpl

function插件

index.tpl

{{insert name='getNow'}}

index.php

function insert_getNow($params,&$smarty){

return time();

}

$smarty->register_function(‘getNow’,'smarty_function_getNow’,false);

register_function的第三个参数设置为false能够阻止插件从缓存中输出

block插件

index.tpl

{{getNow}}

{{/getNow}}

index.php

function smarty_block_getNow($params,&$smarty){

return time();

}

$smarty->register_block(‘getNow’,'smarty_block_getNow’,false);

register_block的第三个参数设置为false能够使用getNow块不缓存

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值