SMARTY靜態緩存

在templates文件夾下建立huancunpage.htmlphp

<!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>
<h1>民族信息</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代號</td>
        <td>名稱</td>
        <td>操做</td>
    </tr>
    
    <{foreach $arr as $v}>
    <tr>
        <td><{$v[0]}></td>
        <td><{$v[1]}></td>
        <td><a href="del.php?code=<{$v[0]}>">刪除</a><a href="update.php?code=<{$v[0]}>">修改</a></td>
    </tr>
    <{/foreach}>
    
</table>
<div><{$page}></div>

<a href="add.php">添加數據</a>

</body>
</html>

 

在main文件夾下建立huancunpage.phphtml

<?php
//當前頁
$p = 1;
if(!empty($_GET["page"]))
{
    $p = $_GET["page"];
}

//定義一個緩存頁面的位置
$filename = "../cache/huancunpage{$p}.html"; //通常緩存文件放在cache文件夾裏 //加緩存時間
$sj = 10; //判斷當前頁面是否要使用緩存
if(file_exists($filename) && (filemtime($filename)+$sj) >time())//注意時間條件的判斷 { //若是存在緩存頁面就顯示緩存文件
    include($filename); } else
{
    //從新生成緩存文件
    ob_start(); //開啓內容緩衝區(緩存)
    
    require("../init.inc.php"); require("../DBDA.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;
    $arr = $db->Query($sql);

    $smarty->assign("arr",$arr);
    $smarty->assign("page",$page->fpage());
    $smarty->display("huancunpage.html");
    
    
    $str = ob_get_contents(); //從內容緩存裏面獲取內容
file_put_contents($filename,$str); //將獲取到的內容放到緩存文件裏面 ob_flush(); //清空內存緩存 echo "########################################";//從新加載時顯示,顯示緩存頁面時不顯示 }

訪問huancunpage.php的顯示結果:sql

 

 第一張是從服務器讀取的數據,第二張是在緩存時間內讀取的緩存文件。緩存

 作分頁的緩存就是把每一張分頁的內容分別緩存。服務器

相關文章
相關標籤/搜索