Smarty之缓存,局部缓存

来源:互联网 发布:人工智能好看吗 编辑:程序博客网 时间:2024/04/30 10:45

smarty缓存

把页面保存到到磁盘,下次访问直接返回保存内容。

不缓存浪费数据库性能,因为刷一次从数据库取一次数据


<?php/*smarty缓存解释:把页面保存到到磁盘,下次访问直接返回保存内容。1.开启缓存2配置缓存的生命周期3配置是否缓存,是否从数据库取数据4输出*/require('../../smarty3/libs/Smarty.class.php');require('./mysmarty.class.php');$smarty=new MySmarty();//开启缓存$smarty->caching=true;//缓存生命周期$smarty->cache_lifetime=20;//缓存目录$smarty->cache_dir='./cache';//判断是否缓存;当没有缓存过或过了生命周期或改了模板就不缓,更新覆盖if(!$smarty->isCached('temp6.html')){$conn=mysqli_connect('localhost','root','123456','boolshop');$sql ='set names utf8';mysqli_query($conn,$sql);$sql ='select * from goods limit 5';$rs=mysqli_query($conn,$sql);$goods=array();while($row=mysqli_fetch_assoc($rs)){$goods[]=$row;}// 不缓存就不经过这块,不会输出下面的字;$smarty->assign('goods',$goods);echo '我不是缓存哟~ o(* ̄▽ ̄*)ブ ';}$smarty->display('temp6.html');?>


局部缓存
1 在一个标签中控制:{$time nocache} ;把标了这个nocache的还是php语言,走php的,看cache文件就知道了
2 控制一片不缓存 {nocache} {/nocache} 
3 php里赋值时控制 $smarty->assign('time',$time,true);
4 用函数控制 

function insert_hi(){return 'hello my world';}
html里

{insert name='hi'}


在函数里还可以加参数

function insert_hi($parm,$smarty){return 'hello my world'." ".$parm['city'];}

html里

<p>{$time}{insert name='hi' city='shanghai'}</p>



0 0
原创粉丝点击