频道栏目
首页 > 资讯 > 缓存 > 正文

PHP之三大缓存:smarty缓存、客户端缓存、PDO缓存之smarty缓存

16-01-06        来源:[db:作者]  
收藏   我要投稿

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块不缓存

 

相关TAG标签
上一篇:49点提高PHP编程效率,引入缓存机制提升性能
下一篇:在Windows上安装PHP的Memcached扩展
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站