php静态文件缓存示例

//开始缓冲区

ob_start();

$cache_file = "./cache/4.3-static.html";
$cache_time = 1;   //设置缓存更新时间

if (file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)) {
  include($cache_file);
  echo "<!--published at " . date("H:i", filemtime($cache_file)) . "by Think Creative CMS -->\n";
  exit();
}

echo '
	<!DOCTYPE html>
	<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Document</title>
	</head>
	<body>
		这是一个缓存文件内容!!
	</body>
	</html>
';

$fp = fopen($cache_file, 'w');

//将输出缓冲区的内容写到文件
fwrite($fp, ob_get_contents());

//关闭文件
fclose($fp);

// 将缓冲区的内容立即输出到浏览器

ob_end_flush();

  

posted @ 2017-08-23 16:56  依旧十八岁的夕阳小子  阅读(294)  评论(0编辑  收藏  举报