三,PHP緩存機制實現頁面靜態化

頁面靜態化思路:php

由於新聞這種信息對實時性要求不高,而且比較穩定,因此能夠這樣作:當地一個用戶訪問某條新聞後,咱們使用ob緩存機制,將內容緩存到html頁面。當下一次訪問時候,直接訪問html頁面。這樣減小訪問數據庫次數,提升程序的效率,可是若是新聞內容修改,html靜態頁面必須實時改變,此處將html靜態頁面設定30s的過時時間,這樣確保hrml靜態頁面和新聞的一直性,可是有30s延遲,無法保證明時性。html

程序代碼以下:sql

(1)數據庫操做的類文件 ConnDB.class.php數據庫

<?php
/**
 * Created by PhpStorm.
 * User: 58
 * Date: 2016/8/5
 * Time: 8:57
 */
class ConnDB{
    private static $host = '127.0.0.1';
    private static $username = 'root';
    private static $password = '7758521Lhy';
    private static $db = 'test';
    private $conn = null;

    public function __construct(){
        $this->conn = new MySQLi(self::$host,self::$username,self::$password,self::$db);
        if(!$this->conn){
            echo '數據庫鏈接錯誤:'.$this->conn->connect_error;
            exit();
        }
        $this->conn->query("set names utf-8");
    }

    public function execute_dql($sql){
        $rs = $this->conn->query($sql) or die('查詢出錯!'.$this->conn->error);
        $rsList = array();
        if($rs){
            while($row = $rs->fetch_assoc()){
                $rsList[] = $row;
            }
        }
        $rs->free();
        return $rsList;
    }

    public function execute_dml($sql){
        $rs = $this->conn->query($sql);
        if(!$rs){
            $flag = 0;
        }else if($this->conn->affected_rows){
            $flag = 1;
        }else{
            $flag = 2;
        }
        return $flag;
    }

    public function clossDB(){
        if($this->conn){
            $this->conn->close();
        }
    }
}

(2)新聞列表顯示頁面 news_list.php緩存

<?php
/**
 * Created by PhpStorm.
 * User: 58
 * Date: 2016/8/5
 * Time: 15:31
 */
header("Content-Type:text/html;charset=utf-8");
require_once "ConnDB.class.php";
$conn = new ConnDB();
$sql = "select * from news";
$rs = $conn->execute_dql($sql);
$conn->clossDB();
ob_start();
echo "
<a href='add_news.php'>發佈文章</a>
<table border='1'>
    <tr><th>id</th><th>標題</th><th>詳細內容</th></tr>";

foreach($rs as $row){
    echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href='show_news.php?id={$row['id']}'>詳細內容</a></td></tr>";
}
echo "
</table>
";

  

(3)單條新聞顯示頁面show_news.phpfetch

<?php
/**
 * Created by PhpStorm.
 * User: 58
 * Date: 2016/8/5
 * Time: 9:40
 */
header("Content-Type:text/html;charset=utf-8");
$id = $_GET['id'];
$html_filename = "news_id".$id.".html";
//新聞若是更新,靜態頁面沒法更新,能夠設定靜態頁面過時時間30s,這樣30s後,從新生成新的靜態頁面,
//這樣保證靜態頁面和新聞的一致性,可是無法確保實時性
if(file_exists($html_filename) && filemtime($html_filename)+30 > time()){
    echo file_get_contents($html_filename);
    exit();
}
require_once "ConnDB.class.php";
$conn = new ConnDB();
$sql = "select * from news where id = {$id} limit 1";
$rs = $conn->execute_dql($sql);
$conn->clossDB();
if($rs){
    ob_start();
    $row = $rs[0];


    echo "
    <table border='1'>
        <tr><th>{$row['title']}</th></tr>
        <tr><td>{$row['content']}</td></tr>
    </table>
    ";
    $html_contents = ob_get_contents();
    $html_header = "<head><meta http-equiv='content-type=text/html;charset=utf-8'></head>";

    file_put_contents($html_filename,$html_header.$html_contents);
}

  

  

單純使用緩存技術存在的不足:ui

(1)news_list.php中點擊「詳細內容」時候,跳轉到html靜態頁面時候,顯示php頁面。好比打開news_id1.html頁面時候,地址欄顯示http://127.0.0.1/show_news.php?id=1this

(2)實時性不完美,存在30s延時。orm

可使用真靜態技術解決此問題。htm

相關文章
相關標籤/搜索