简单案例:
ob缓存
public function l(){
ob_start();
if(!is_file("3.html") | time()-filemtime('3.html')>5){
echo "haoyunyun";
$content=ob_get_contents();
$fb=fopen("3.html",'w');
fwrite($fb,$content);
fclose($fb);
}else{
echo "This is my article";
echo "</br>";
echo file_get_contents("3.html");
}
}
ob缓存
ob_start(); //打开输出缓冲区
$cacheTime = 864000; //设置缓存页面过期时间
$cacheDir = 'Cache'; //设置缓存页面文件目录
if (!is_dir($cacheDir)) mkdir($cacheDir); //判断目录是否存在,否则创建目录
$cacheFile = $cacheDir.'/cache_'.$goods_id.'.dwt'; //缓存文件路径,文件以日期命名
if (!is_file($cacheFile) || time() - filemtime($cacheFile) > $cacheTime) {
$smarty->display('goods.dwt');
$content = ob_get_contents(); //取得php页面输出的全部内容
$fp = fopen($cacheFile, "w"); //输出内容写入文件
fwrite($fp, $content);
fclose($fp);
} else {
echo '缓存'.$content = file_get_contents($cacheFile); //如果缓存文件已经存在,且未过期则读取
}