zoukankan      html  css  js  c++  java
  • 缓存

    后台页面:

    <?php
    //缓存:不读数据库,直接显示一个静态页面。优点访问速度快,缺点不能及时的更新显示,用在对数据库操作对于用户浏览体验不影响的地方,缓存文件在cache文件夹里面(把缓存好的静态页面直接显示出来)
    
    //1.定义一个该页面的缓存文件路径
    $filename = "../cache/testhuancun.html";
    
    //2.设置缓存时间
    $time=5;
    
    //3.判断这个缓存文件是否存在,存在读取缓存文件,不存在走查询数据库代码
    if(!file_exists($filename) || filemtime($filename)+$time<time())//缓存文件不存在或者修改时间+5秒小于当前时间的时候走if,设置5秒的缓存间隔时间
    {
        //开启内存缓存
        ob_start();
        include("../init.inc.php");
        include("../DBDA.php");
        $db = new DBDA();
        
        $sql = "select * from nation";
        $attr = $db->Query($sql);
        
        $smarty->assign("nation",$attr);
        $smarty->display("test.html");
        
        //把内存里面的内容读出来
        $nr = ob_get_contents();
        
        //将读到的内容存放到缓存文件
        file_put_contents($filename,$nr);
        
        //清除内存缓存
        ob_flush();
        
        echo "##############################";    //如果显示echo输出的内容,说明5秒缓存时间已过期,用户再浏览网页,走数据库,如果不显示的话说明缓存还没到期,走else,直接调取静态页面显示
    }
    else
    {
        include($filename);
    }

    静态页面:

    <body>
    <table width="100%" border="1">
        <tr><td>代号</td><td>名称</td></tr>
        <{foreach $nation as $v}>
            <tr>
                <td><{$v[0]}></td>
                <td><{$v[1]}></td>
            </tr>
        <{/foreach}>
    </table>
    </body>
  • 相关阅读:
    从头到尾彻底理解KMP
    [CF1220E] Tourism
    [CF446C] DZY Loves Fibonacci Numbers
    [CF1003E] Tree Constructing
    [CF1238E] Keyboard Purchase
    [CF915E] Physical Education Lessons
    [CF788B] Weird journey
    [CF1371E2] Asterism (Hard Version)
    [CF780E] Underground Lab
    [CF372C] Watching Fireworks is Fun
  • 原文地址:https://www.cnblogs.com/zxl89/p/6210618.html
Copyright © 2011-2022 走看看