php7中的php.ini 默认开启
output_buffering = 4096
例子:
<?php
include_once 'common/common.php';//数据库操作方法
$fileName = 'test.html';
$expr = 10;//缓存时间
$time_start = microtime();
if( file_exists($fileName)){//判断文件是否存在
$fileCreateTime = filectime($fileName);
if ($fileCreateTime+$expr > time()) {//判断缓存文件是否过期
/*没有过期,输出缓存文件*/
echo file_get_contents($fileName);
$time_end = microtime();
echo '<br>查询静态页面花费的时间:';
echo $time_end - $time_start;
die();
}else{
/*删除过期文件*/
unlink($fileName);
}
}
ob_start();
$sql = "SELECT classname FROM category WHERE cid > 0";
$res = mySelect($conn,$sql);
foreach ($res as $k => $v){
echo "<a href='http://www.baidu.com'>".$v['classname']."</a>"."<br>";
}
echo date('Y-m-d H:i:s',time());
$time_end = microtime();
echo '<br>查询数据库花费的时间:';
echo $time_end - $time_start;
/*将页面缓存起来*/
$content = ob_get_contents();
$fp = fopen('test.html','w');
fwrite($fp,$content);
fclose($fp);
ob_end_clean();
从输出时间差发现,加载静态页面速度远远大于动态页面
参考:http://www.jb51.net/article/74342.htm
http://www.jb51.net/article/47951.htm