静态缓存

<?php

$filename = "../cache/test.html"; //缓存文件的地址
$youxiao = 3; //定义缓存有效期为3秒

//判断缓存文件是否存在
if(file_exists($filename) && (filemtime($filename)+$youxiao)>time()){
    //如果存在,直接读取缓存文件
    include($filename);
}else{
    ob_start(); //开启内存缓存

    require "../DBDA.class.php";
    require "../init.inc.php";

    $db = new DBDA();

    $sql = "select * from info";
    $arr = $db->query($sql);

    $smarty->assign("shuju",$arr);
    $smarty->display("test.html");

    $str = ob_get_contents(); //获取内存中的静态页面
    file_put_contents($filename,$str); //将获取到的内容写入到缓存文件

    ob_flush(); //输出内存并且释放
    echo "#####################################";
}

 

posted @ 2017-07-02 10:03  繁华似水  阅读(98)  评论(0编辑  收藏  举报