制作smarty模版缓存文件
<?php
$p = 1;
if(!empty($_GET["page"]))
{
$p = $_GET["page"];
}
$filename = "../cache/huancun{$p}.html"; //缓存文件存放的位置
$time = 30; //缓存有效期10秒
/*var_dump((filemtime($filename)+$time)<= time());
echo filemtime($filename)+$time;
echo time();*/
//判断缓存文件是否存在,如果缓存文件存在直接调用缓存,如果缓存文件不存在重新缓存
if(file_exists($filename) && ((filemtime($filename)+$time)>= time()) )
{
//直接调用缓存
include($filename);
}
else
{
//重新缓存
ob_start(); //开启内存缓存
include("../init.inc.php");
include("../DBDA.php");
$db = new DBDA();
$sall = "select count(*) from nation";
$zts = $db->StrQuery($sall);
include("../page.class.php");
$page = new Page($zts,5);
$sql = "select * from nation ".$page->limit;
$arr = $db->Query($sql);
$smarty->assign("fpage",$page->fpage());
$smarty->assign("shuzu",$arr);
$smarty->display("huancun.html");
$str = ob_get_contents(); //获取内存中的缓存内容
file_put_contents($filename,$str);
ob_flush(); //关闭内存缓存
echo "#######################################";
}