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>
  • 相关阅读:
    WWDC2014苹果的“软件”发布会
    那些好用的iOS开发工具
    40个国人iOS技术博客
    深度解析开发项目之 05
    深度解析开发项目之 04
    深度解析开发项目之 03
    深度解析开发项目之 02
    成长为 iOS 大 V 的秘密
    理解 iOS 的内存管理
    峰回路转的提权08r2服务器
  • 原文地址:https://www.cnblogs.com/anArtist/p/5327366.html
Copyright © 2011-2022 走看看