php真静态缓存 ob系统函数

来源:互联网 发布:多元线性回归模型矩阵 编辑:程序博客网 时间:2024/05/16 12:02

真静态缓存:(真的生成一个html页面,在接下来的操作中直接调用该html)
第一次(刷新网页面)判断是否存在缓存文件 (可设置一时间)
如果不存在 第一次是(可根据PDO)连接数据库 查找数据 输出到页面 并且同时缓存(写入)到本地一个.html文件;
再次(刷新网页面) 判断是否存有该缓存文件时
当存在缓存文件,直接查找本地缓存(file_get_content) 读取出来

以下代码在ci框架中编写完成

$time = 10; //设置缓存页面过期时间if(!is_file("obtest.html") || time()-filemtime("obtest.html")>$time){ob_start();       //打开缓冲区   $arr['res']=$this->db->get('user')->result_array();$this->load->view('welcome_message',$arr);//print_r($arr);$info = ob_get_contents();       //得到缓冲区的内容并且赋值给$info   $file = fopen('obtest.html', 'w');  //打开文件info.txt   fwrite($file, $info);       //写入信息到info.txt   fclose($file);          //关闭文件info.txt}else{    echo "cache:";    echo file_get_contents("obtest.html");}

fopen 和 filemtime 方法的使用:

fopen() 函数打开文件或者 URL。fopen(文件名称,文件访问类型)w写入方式打开。filemtime() 函数返回文件内容上次的修改时间,里面跟文件名。
0 0
原创粉丝点击