PHP页面缓存简单实现

<?php
//文件名
$filename="filename.html";
//文件路径,DIRECTORY_SEPARATOR适合Linux以及Windows
$fileabs = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $filename;
//查找有没有缓存文件的存在
if (file_exists($fileabs)) {
  //有缓存文件直接调用
  include $fileabs;
  //获取当前时间戳
  $now_time = time();
  //获取缓存文件时间戳
  $last_time = filemtime($fileabs);
  //如果缓存文件生成超过指定的时间直接删除文件
  if (($now_time - $last_time) / 60 > 30) {
    unlink($fileabs);
  }
  exit;
}
//开启缓存
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<body>
	<!--html内容代码-->
</body>
</html>
<?php
//在文件代码末尾获取上面生成的缓存内容
$content = ob_get_contents();
//写入到缓存内容到指定的文件夹
$fp = fopen($fileabs, 'w');
fwrite($fp, $content);
fclose($fp);
ob_flush(); //从PHP内存中释放出来缓存(取出数据)
flush(); //把释放的数据发送到浏览器显示
ob_end_clean(); //清空缓冲区的内容并关闭这个缓冲区
?>

猜你喜欢

转载自blog.csdn.net/chenxianxing/article/details/87616684
今日推荐