欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)網(wǎng)頁(yè)緩存的工具類分享

 更新時(shí)間:2015年07月14日 11:56:16   投稿:hebedich  
本文給大家分享的是php實(shí)現(xiàn)網(wǎng)頁(yè)緩存的工具類的代碼及使用方法,非常的實(shí)用,有需要的小伙伴可以參考下。

php程序在抵抗大流量訪問(wèn)的時(shí)候動(dòng)態(tài)網(wǎng)站往往都是難以招架,所以要引入緩存機(jī)制,一般情況下有兩種類型緩存

一、文件緩存

二、數(shù)據(jù)查詢結(jié)果緩存,使用內(nèi)存來(lái)實(shí)現(xiàn)高速緩存

本例主要使用文件緩存。

主要原理使用緩存函數(shù)來(lái)存儲(chǔ)網(wǎng)頁(yè)顯示結(jié)果,如果在規(guī)定時(shí)間里再次調(diào)用則可以加載緩存文件。

工具類代碼:

// 文件緩存類
class Cache {
  /**
   * $dir : 緩存文件存放目錄
   * $lifetime : 緩存文件有效期,單位為秒
   * $cacheid : 緩存文件路徑,包含文件名
   * $ext : 緩存文件擴(kuò)展名(可以不用),這里使用是為了查看文件方便
   */
  private $dir;
  private $lifetime;
  private $cacheid;
  private $ext;
  /**
   * 析構(gòu)函數(shù),檢查緩存目錄是否有效,默認(rèn)賦值
   */
  function __construct($dir = '', $lifetime = 1800) {
    if ($this->dir_isvalid ( $dir )) {
      $this->dir = $dir;
      $this->lifetime = $lifetime;
      $this->ext = '.Php';
      $this->cacheid = $this->getcacheid ();
    }
  }
  /**
   * 檢查緩存是否有效
   */
  private function isvalid() {
    if (! file_exists ( $this->cacheid ))
      return false;
    if (! (@$mtime = filemtime ( $this->cacheid )))
      return false;
    if (mktime () - $mtime > $this->lifetime)
      return false;
    return true;
  }
  /**
   * 寫入緩存
   * $mode == 0 , 以瀏覽器緩存的方式取得頁(yè)面內(nèi)容
   * $mode == 1 , 以直接賦值(通過(guò)$content參數(shù)接收)的方式取得頁(yè)面內(nèi)容
   * $mode == 2 , 以本地讀取(fopen ile_get_contents)的方式取得頁(yè)面內(nèi)容(似乎這種方式?jīng)]什么必要)
   */
  public function write($mode = 0, $content = '') {
    switch ($mode) {
      case 0 :
        $content = ob_get_contents ();
        break;
      default :
        break;
    }
    ob_end_flush ();
    try {
      file_put_contents ( $this->cacheid, $content );
    } catch ( Exception $e ) {
      $this->error ( '寫入緩存失敗!請(qǐng)檢查目錄權(quán)限!' );
    }
  }
  /**
   * 加載緩存
   * exit() 載入緩存后終止原頁(yè)面程序的執(zhí)行,緩存無(wú)效則運(yùn)行原頁(yè)面程序生成緩存
   * ob_start() 開(kāi)啟瀏覽器緩存用于在頁(yè)面結(jié)尾處取得頁(yè)面內(nèi)容
   */
  public function load() {
    if ($this->isvalid ()) {
      // 以下兩種方式,哪種方式好?????
      require_once ($this->cacheid);
      echo "<!--緩存-->";
      // echo file_get_contents($this->cacheid);
      exit ();
    } else {
      ob_start ();
    }
  }
  /**
   * 清除緩存
   */
  public function clean() {
    try {
      unlink ( $this->cacheid );
    } catch ( Exception $e ) {
      $this->error ( '清除緩存文件失敗!請(qǐng)檢查目錄權(quán)限!' );
    }
  }
  /**
   * 取得緩存文件路徑
   */
  private function getcacheid() {
    return $this->dir . md5 ( $this->geturl () ) . $this->ext;
  }
  /**
   * 檢查目錄是否存在或是否可創(chuàng)建
   */
  private function dir_isvalid($dir) {
    if (is_dir ( $dir ))
      return true;
    try {
      mkdir ( $dir, 0777 );
    } catch ( Exception $e ) {
      $this->error ( '所設(shè)定緩存目錄不存在并且創(chuàng)建失敗!請(qǐng)檢查目錄權(quán)限!' );
      return false;
    }
    return true;
  }
  /**
   * 取得當(dāng)前頁(yè)面完整url
   */
  private function geturl() {
    $url = '';
    if (isset ( $_SERVER ['REQUEST_URI'] )) {
      $url = $_SERVER ['REQUEST_URI'];
    } else {
      $url = $_SERVER ['Php_SELF'];
      $url .= empty ( $_SERVER ['QUERY_STRING'] ) ? '' : '?' . $_SERVER ['QUERY_STRING'];
    }
    return $url;
  }
  /**
   * 輸出錯(cuò)誤信息
   */
  private function error($str) {
    echo '<div style="color:red;">' . $str . '</div>';
  }
}

使用方法:

使用方法如下:

一部分代碼放在要被緩存邏輯代碼前面:

$cachedir = './Cache/'; // 設(shè)定緩存目錄
    $cache = new Cache ( $cachedir, 33 ); // 省略參數(shù)即采用缺省設(shè)置, $cache = new Cache($cachedir);
    if (@$_GET ['cacheact'] != 'rewrite' || @$_GET ['clearCache'] == 'ok') // 此處為一技巧,通過(guò)xx.Php?cacheact=rewrite更新緩存,以此類推,還可以設(shè)定一些其它操作
      $cache->load (); // 裝載緩存,緩存有效則不執(zhí)行以下頁(yè)面代碼
    // 頁(yè)面代碼開(kāi)始

一部分放在被緩存邏輯代碼后面:

// 頁(yè)面代碼結(jié)束
    $cache->write (); // 首次運(yùn)行或緩存過(guò)期,生成緩存

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論