+
95
-

回答

如果是想在服务器端缓存生成的html页面的话,可以检测页面生成时间,设定过期时间,然后来输出,代码如下:

<?php

if(is_file('./index.html') && (time()-filemtime('./index.html')) < 60){
// 假设缓存时间是60秒
// 获取页面
require_once('./index.html');
}else{
// 重新生成一份静态页面
// 准备要展示到网页的数据
$data = array(
array('id'=>1,'msg'=>'hello java'),
array('id'=>2,'msg'=>'hello php'),
array('id'=>3,'msg'=>'hello python'),
);

// 渲染到模板
// 实际项目一般是在html里渲染
// 这里演示 希望能看懂

ob_start(); // 开始输入缓冲控制

foreach($data as $item){
echo $item['id'].'===>'.$item['msg'].'<br/>';
}

// 开始生成静态页面文件
file_put_contents('index.html',ob_get_contents());
}


网友回复

我知道答案,我要回答