smarty模板缓存二

来源:互联网 发布:淘宝主订单不可以评价 编辑:程序博客网 时间:2024/06/06 03:40

今天在经过smarty的初步了解实现了单页面单缓存和多页面多缓存。
单页面单缓存:
 显示同样的页面,同样的内容,不需要再执行查询获取数据和加载的过程,

直接读取缓存。
单页面多缓存:
 显示同样的页面,但是显示内容不同,同一个模板可以生成多个缓存,此时

通过传递的参数作为缓存的id来确定是哪个缓存。
设置缓存的id:
 1.通过display()函数的参数决定模板,以及id。fetch方法生成缓存文件的格

式与display相同。
 2.在php文件中用is_cached()判断模板是否被缓存,设置id参数
练习部分代码事例:
 <?php
 include ("libs/Smarty.class.php");
 $smarty=new Smarty();//创建对象
 $smarty->InitSmarty("demo/templates","demo/templates_c","demo/config");
 //开启缓存
 $smarty->caching=1;
 //设置缓存路径
 $smarty->cache_dir='demo/cache';
 //判断是否已经存在缓存
 if(!$smarty->is_cached("information.tpl",$_GET['id'])){
 mysql_connect("localhost","root","123") or die("数据库连接错误");
 mysql_select_db("ec_shop");
 mysql_query("set names utf8");
 $id=$_GET['id'];
 $result=mysql_query("select * from brand where id='$id'");
 $return=array();
 while($row=mysql_fetch_assoc($result))
 {
  $return[]=$row;
 }
 //var_dump($return);
 $smarty->assign("array",$return);
 echo  "模板没有被缓存、";
 }
 $smarty->display("information.tpl",$_GET['id']);//第二个参数是缓存文件的

id用来区别缓存文件

原创粉丝点击