php ob静态缓存

<?php   
ob_start(); //打开输出缓冲区  
$cacheTime = 864000; //设置缓存页面过期时间  
$cacheDir = 'cacheDir'; //设置缓存页面文件目录  
if (!is_dir($cacheDir)) mkdir($cacheDir); //判断目录是否存在,否则创建目录  
$cacheFile = $cacheDir.'/'.(int)date("Ymd").'.html'; //缓存文件路径,文件以日期命名  
if (!is_file($cacheFile) || time() - filemtime($cacheFile) > $cacheTime) {  
<!--页面输出部分内容。也是ob_get_contents()函数取得的全部内容-->  
$content = ob_get_contents(); //取得php页面输出的全部内容   
$fp = fopen($cacheFile, "w"); //输出内容写入文件  
fwrite($fp, $content);  
fclose($fp);  
} else {  
echo $content = file_get_contents($cacheFile); //如果缓存文件已经存在,且未过期则读取  
}  
?>  

  

原文地址:https://www.cnblogs.com/comsky/p/5857677.html