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

php實(shí)現(xiàn)的redis緩存類(lèi)定義與使用方法示例

 更新時(shí)間:2017年08月09日 11:47:53   作者:風(fēng)火程序員  
這篇文章主要介紹了php實(shí)現(xiàn)的redis緩存類(lèi),結(jié)合具體實(shí)例形式分析了php封裝的針對(duì)redis緩存類(lèi)定義與使用相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)的redis緩存類(lèi)定義與使用方法。分享給大家供大家參考,具體如下:

php+redis緩存類(lèi)

<?php
class redisCache {
  /**
  * $host : redis服務(wù)器ip
  * $port : redis服務(wù)器端口
  * $lifetime : 緩存文件有效期,單位為秒
  * $cacheid : 緩存文件路徑,包含文件名
  */
  private $host;
  private $port;
  private $lifetime;
  private $cacheid;
  private $data;
  public $redis;
  /**
  * 析構(gòu)函數(shù),檢查緩存目錄是否有效,默認(rèn)賦值
  */
  function __construct($lifetime=1800) {
    //配置
    $this->host = "127.0.0.1";
    $this->port = "6379";
    $redis = new Redis();
    $redis->pconnect($this->host,$this->port);
    $this->redis=$redis;
    $this->cacheid = $this->getcacheid();
    $this->lifetime = $lifetime;
    $this->data=$redis->hMGet($this->cacheid, array('content','creattime'));
    //print_r($this->redis);
    //print_r($this->data);
  }
  /**
  * 檢查緩存是否有效
  */
  private function isvalid(){
    $data=$this->data;
    if (!$data['content']) return false;
    if (time() - $data['creattime'] > $this->lifetime) return false;
    return true;
  }
  /**
  * 寫(xiě)入緩存
  * $mode == 0 , 以瀏覽器緩存的方式取得頁(yè)面內(nèi)容
  */
  public function write($mode=0,$content='') {
    switch ($mode) {
      case 0:
        $content = ob_get_contents();
        break;
      default:
        break;
    }
    ob_end_flush();
    try {
      $this->redis->hMset($this->cacheid, array('content'=>$content,'creattime'=>time()));
      $this->redis->expireAt($this->cacheid, time() + $this->lifetime);
    }
    catch (Exception $e) {
      $this->error('寫(xiě)入緩存失敗!');
    }
  }
  /**
  * 加載緩存
  * exit() 載入緩存后終止原頁(yè)面程序的執(zhí)行,緩存無(wú)效則運(yùn)行原頁(yè)面程序生成緩存
  * ob_start() 開(kāi)啟瀏覽器緩存用于在頁(yè)面結(jié)尾處取得頁(yè)面內(nèi)容
  */
  public function load() {
    if ($this->isvalid()) {
      echo $this->data['content'];
      exit();
    }
    else {
      ob_start();
    }
  }
  /**
  * 清除緩存
  */
  public function clean() {
    try {
      $this->redis->hDel($this->cacheid, array('content','creattime'));
    }
    catch (Exception $e) {
      $this->error('清除緩存失敗!');
    }
  }
  /**
  * 取得緩存文件路徑
  */
  private function getcacheid() {
    return $this->dir.md5($this->geturl()).$this->ext;
  }
  /**
  * 取得當(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>';
  }
}
//用法:
// require_once('redisCache.php');
// $cache = new redisCache(10); //設(shè)置緩存生存期
// if ($_GET['clearCache']) $cache->clean();
// else $cache->load(); //裝載緩存,緩存有效則不執(zhí)行以下頁(yè)面代碼
// //頁(yè)面代碼開(kāi)始
// //頁(yè)面代碼結(jié)束
// $cache->write(); //首次運(yùn)行或緩存過(guò)期,生成緩存
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論