zoukankan      html  css  js  c++  java
  • 动态网页静态化+局部静态化

    静态化:

    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>
  • 相关阅读:
    SpringCloud笔记(一)服务注册与发现
    个人备忘录
    ActiveMQ 消息持久化到Mysql数据库
    染色 [组合数 容斥]
    各种图床
    NOIP2012 疫情控制
    网格计数
    找钱 [多重背包 计数]
    序列[势能线段树]
    牛客挑战赛33 B-鸽天的放鸽序列
  • 原文地址:https://www.cnblogs.com/anArtist/p/5327366.html
Copyright © 2011-2022 走看看