smarty缓存

不分页缓存

模板页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<div>汽车表</div>
<table  bgcolor="#FF0000" cellpadding="1px" cellspacing="0" border="1px">
<tr bgcolor="#CCCCCC">
   <td>代号</td>
   <td>名称</td>
   <td>系列</td>
   <td>上市时间</td>
   <td>油耗</td>
   <td>功率</td>
</tr>
<{foreach $xinxi as $v}>
<tr  bgcolor="#66FFFF">
   <td><{$v[0]}></td>
   <td><{$v[1]}></td>
   <td><{$v[2]}></td>
   <td><{$v[3]}></td>
   <td><{$v[4]}></td>
   <td><{$v[5]}></td>
</tr>
<{/foreach}>
</table>
</body>
</html>

php页面

<?php
include ("../DBDA.php");
include ("../init.inc.php");
include ("../page.class.php");
//取当前页
if ($_GET["page"]="")
{
    $p=0;
}
$p=$_GET["page"];
//定义该页面缓存文件存放路径

$filename="../cache/cachetest[$p].html";
//定义缓存有效期
$cachetime=2;
if (!file_exists($filename)||filemtime($filename)+$cachetime<time())
{
    //开启内存缓存
    ob_start ();

$db=new DBDA();
//求总条数
$szong = "select count(*) from car";
$azong = $db->Query($szong);
$zongshu = $azong[0][0]; //总条数
//造分页对象
$page=new Page($zongshu,5);
//在SQL语句拼接分页条件
$sql= "select * from car ".$page->limit;
$fenye = $db->Query($sql);
//返回分页信息
$tj=$page->fpage(4,5,6);
//将分页结果数组添加到Smarty中
$smarty->assign("tiaojian",$tj);
$smarty->assign("xinxi",$fenye);
$smarty->display("car1.html");
     //从内存缓存中获取页面代码
     $content =ob_get_contents();
     //将获取到的内容存放到缓存文件
     file_put_contents($filename,$content);     
     //清除内存缓存
     ob_flush();
    echo "标识";    //区分是否是缓存
}
else 
{   //读取缓存文件
    include ($filename);
}

分页缓存

模板页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

</head>

<body>
<div>汽车表</div>
<table  bgcolor="#FF0000" cellpadding="1px" cellspacing="0" border="1px">
<tr bgcolor="#CCCCCC">
   <td>代号</td>
   <td>名称</td>
   <td>系列</td>
   <td>上市时间</td>
   <td>油耗</td>
   <td>功率</td>
</tr>
<{foreach $xinxi as $v}>
<tr  bgcolor="#66FFFF">
   <td><{$v[0]}></td>
   <td><{$v[1]}></td>
   <td><{$v[2]}></td>
   <td><{$v[3]}></td>
   <td><{$v[4]}></td>
   <td><{$v[5]}></td>
</tr>
<{/foreach}>
</table>
<div><{$tiaojian}></div>
</body>
</html>

php页面

<?php
include ("../DBDA.php");
include ("../init.inc.php");
//定义该页面缓存文件存放路径    
$filename="../cache/cachetest.html";
//定义缓存有效期
$cachetime=2;
if (!file_exists($filename)||filemtime($filename)+$cachetime<time())
{
    //开启内存缓存
    ob_start ();

$db=new DBDA();
$sql="select * from car";
$attr=$db->Query($sql);
$smarty->assign("xinxi",$attr);
$smarty->display("car.html");
     //从内存缓存中获取页面代码
     $content =ob_get_contents();
     //将获取到的内容存放到缓存文件
     file_put_contents($filename,$content);     
     //清除内存缓存
     ob_flush();
    echo "标识";    
}
else 
{
    include ($filename);
}

 

posted on 2016-07-26 20:38  。。小兵  阅读(147)  评论(0编辑  收藏  举报