1. 程式人生 > >7.20php文件緩存

7.20php文件緩存

第一次 sele dump add .html ctype clas mysqli nal

<?php
    $filename = "./jingtai.html";//定義一個變量來存放一個緩存文件的路徑(相對於當前頁面的路徑)
    //定義緩存有效期
    $cachetime = 10;

    //判斷緩存文件是否存在                                   
    var_dump(filemtime($filename));
                                //filemtime($filename)不變,time()一直變,filemtime($filename)+10肯定會有小於time()的時候。
    if
(!file_exists($filename)||filemtime($filename)+$cachetime<time()){//filemtime($filename)獲取文件修改時間,加上定義的緩存時間小於當前時間 //開啟內存緩存 //filemtime()該文件上次修改內容 //filemtime($filename)第一次出現等於當前時間time();每過10秒走一次函數,filemtime($filename)的值就會改變 var_dump
(time()); ob_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <table width="100%" border="1" cellpadding="0" cellspacing="0" > <tr> <td>學號</td> <td>姓名</td> <td>性別</td> <td>生日</td> <td>班級</td> </tr> <?php
//造連接對象 $db = new MySQLi("localhost","root","","ceshi"); //寫SQL語句 $sql = "select * from student"; //執行 $result = $db->query($sql); while($attr = $result->fetch_row()){ echo "<tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td>{$attr[2]}</td> <td>{$attr[3]}</td> <td>{$attr[4]}</td> </tr>"; } ?> </table> </body> </html> <?php //從內存緩存中獲取頁面代碼 $content = ob_get_contents(); //將獲取到的內容存放到緩存文件 file_put_contents($filename,$content); //清掉內存緩存 ob_flush(); echo "#####"; //測試是否調用了緩存文件,緩存文件不輸出這句話 } else{ //var_dump(filemtime($filename)); var_dump(time()); include($filename); //如果存在,調用緩存文件 } ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無標題文檔</title>
    </head>
    <body>
        <table width="100%" border="1" cellpadding="0" cellspacing="0" >
            <tr>
                <td>學號</td>
                <td>姓名</td>
                <td>性別</td>
                <td>生日</td>
                <td>班級</td>
            </tr>         
<tr>
                    <td>1</td>
                    <td>101</td>
                    <td>王超</td>
                    <td>男</td>
                    <td>2000-02-12 00:00:00</td>
                    </tr><tr>
                    <td>2</td>
                    <td>103</td>
                    <td>陸君</td>
                    <td>男</td>
                    <td>1974-06-03 00:00:00</td>
                    </tr><tr>
                    <td>3</td>
                    <td>105</td>
                    <td>匡明</td>
                    <td>男</td>
                    <td>1975-10-02 00:00:00</td>
                    </tr><tr>
                    <td>4</td>
                    <td>107</td>
                    <td>王麗</td>
                    <td>女</td>
                    <td>1976-01-23 00:00:00</td>
                    </tr><tr>
                    <td>5</td>
                    <td>108</td>
                    <td>曾華</td>
                    <td>男</td>
                    <td>1977-09-01 00:00:00</td>
                    </tr><tr>
                    <td>6</td>
                    <td>109</td>
                    <td>王芳</td>
                    <td>女</td>
                    <td>1975-02-10 00:00:00</td>
                    </tr><tr>
                    <td>7</td>
                    <td>110</td>
                    <td>劉備</td>
                    <td>男</td>
                    <td>0000-00-00 00:00:00</td>
                    </tr><tr>
                    <td>8</td>
                    <td>111</td>
                    <td>孫權1</td>
                    <td>男</td>
                    <td>0000-00-00 00:00:00</td>
                    </tr><tr>
                    <td>9</td>
                    <td>112</td>
                    <td>曹操</td>
                    <td>男</td>
                    <td>0000-00-00 00:00:00</td>
                    </tr><tr>
                    <td>10</td>
                    <td>113</td>
                    <td>劉邦</td>
                    <td>男</td>
                    <td>0000-00-00 00:00:00</td>
                    </tr><tr>
                    <td>11</td>
                    <td>114</td>
                    <td>周瑜</td>
                    <td>男</td>
                    <td>0000-00-00 00:00:00</td>
                    </tr><tr>
                    <td>17</td>
                    <td>098</td>
                    <td>zhangsan32</td>
                    <td>男</td>
                    <td>0000-00-00 00:00:00</td>
                    </tr>        </table> 
    </body>
    </html>

7.20php文件緩存