smarty的缓冲

首先在main文件夹里面建一个文件 huancun.php   然后在template文件夹里面建一个文件 huancun.html  

huancun.php中的内容为:

require("../init.inc.php");
    require("../DBDA.class.php");;

    $db = new DBDA();

    $sql="select * from nation ";
    $arr=$db->Query($sql);

    $smarty->assign("arr",$arr);
    $smarty->display("huancun.html");

huancun.html中的内容为:

<table width="100%" cellpadding="0" cellspacing="0" border="1">
    <tr>
        <td>代号</td>
        <td>名称</td>
        <td>操作</td>
    </tr>
    <{foreach $arr as $v}>
    <tr>
        <td><{$v[0]}></td>
        <td><{$v[1]}></td>
        <td><a href="shanchu.php?code=<{$v[0]}>">删除</a>
            <a href="xiugai.php?code=<{$v[0]}>">修改</a> </td>
    </tr>
    <{/foreach}>

</table>

<a href="tianjia.php">添加</a>

在这个基础上 我要建立一个缓存文件,那么就要在huncun.php中做修改了,同时也在其基础上做了分页的缓存处理

<?php
//定义当前位置所在的页数
$p=1;
if(!empty($_GET["page"]))
{
    $p=$_GET["page"];
}

//定义一个缓存页面的位置,通常放到cache文件中
$filename="../cache/huancunpage{$p}.html";

//定义缓存的时间   filemname 指的是缓存文件的修改时间
$tj=10;

//判断当前页面是否需要缓存
if(file_exists($filename)&&filemtime($filename)+$tj>=time())
{
    //如果存在缓存页面 就显示缓存
    include($filename);
}
else{
    //重新生成缓存
    ob_start();//开启缓存

    require("../init.inc.php");
    require("../DBDA.class.php");;
    require("../page.class.php");

    $db = new DBDA();
    $ztss = "select count(*) from nation";
    $zts = $db->StrQuery($ztss);

    $page = new Page($zts,5);


    $sql="select * from nation ".$page->limit;//注意一下nation后面的空格 这个空格是必须要有的
    $arr=$db->Query($sql);

    $smarty->assign("page",$page->fpage());
    $smarty->assign("arr",$arr);
    $smarty->display("huancun.html");

    $str=ob_get_contents();//从内存中获取内容
    file_put_contents($filename,$str);//把获取到的内容放到缓存文件里边
    ob_flush();//清除缓存

    echo"######"; //这里是为了测试是不是有缓存

}
原文地址:https://www.cnblogs.com/xiaodouding/p/6774686.html