动态网页静态化+局部静态化

静态化:

1.设置缓存时间

if(is_file('./index.html') && (time()-filemtime('./index.html') < 1200)) {
    require_once('./index.html');
}else {
    // 引入数据库链接操作
    require_once('./db.php');
    $sql = "select * from news where `category_id` = 1 and `status` = 1 limit 4";
    try{
        $db = Db::getInstance()->connect();
        $result = mysql_query($sql, $db);
        $newsList = array();
        while($row = mysql_fetch_assoc($result)) {
            $newsList[] = $row;
        }
    }catch(Exception $e) {
        // TODO
    }
    ob_start();
    require_once('template/index.php');
    $s = ob_get_contents();
    file_put_contents('./index.html', $s);
    //ob_clean();
}

 

 因为是缓存设置的时间,则在此之前的缓存不能被清除,所以在此前生成静态文件的时候,获取缓存内容就不能用ob_get_clean()函数,这个函数获取缓存内容的后清除了缓存,但是ob_get_contents()就会获取缓存不会清除缓存。所以要用ob_get_contents();

2.加个按钮选择手动刷新

3.crontab -e 定时 ,分 时 日 月 周 执行语句

 

局部更新:

ajax的get方法,拿接口,写个js,获取接口数据然后格式化。

<script>
$.ajax({ 
    type : "GET",
    url: "http://static.com/demo4/api/ajax.php",
    dataType : "json",
    success: function(data){
        alert(data.message);
    }
});
</script>

 

posted on 2016-03-27 23:21  阿卡贝拉  阅读(791)  评论(0编辑  收藏  举报