一個不錯的PHP檔案頁面緩衝類

來源:互聯網
上載者:User

頁面緩衝類

 代碼如下 複製代碼

<?php   
/*   
* 緩衝類    cache  
* 作    者:多菜鳥  
* 實    例:  
*/  
/*include( "cache.php" );  
   
$cache = new cache(30);  
$cache->cacheCheck();  
   
echo date("Y-m-d H:i:s");  
   
$cache->caching();  */

class cache {   
  //緩衝目錄   
  var $cacheRoot        = "./cache/";   
  //緩衝更新時間秒數,0為不緩衝   
  var $cacheLimitTime   = 3;
  //快取檔案名   
  var $cacheFileName    = "";   
  //緩衝副檔名   
  var $cacheFileExt     = "php";   
    
  /*  
   * 建構函式  
   * int $cacheLimitTime 緩衝更新時間  
   */  
  function cache( $cacheLimitTime ) {   
    if( intval( $cacheLimitTime ) )    
      $this->cacheLimitTime = $cacheLimitTime;   
    $this->cacheFileName = $this->getCacheFileName();   
    ob_start();   
  }   
     
  /*  
   * 檢查快取檔案是否在設定更新時間之內  
   * 返回:如果在更新時間之內則返迴文件內容,反之則返回失敗  
   */  
  function cacheCheck(){   
    if( file_exists( $this->cacheFileName ) ) {   
      $cTime = $this->getFileCreateTime( $this->cacheFileName );   
      if( $cTime + $this->cacheLimitTime > time() ) {   
        echo file_get_contents( $this->cacheFileName );   
        ob_end_flush();   
        exit;   
      }   
    }   
    return false;   
  }   
    
  /*  
   * 快取檔案或者輸出靜態  
   * string $staticFileName 靜態檔案名稱(含相對路徑)  
   */  
  function caching( $staticFileName = "" ){   
    if( $this->cacheFileName ) {   
      $cacheContent = ob_get_contents();   
      //echo $cacheContent;   
      ob_end_flush();   
    
      if( $staticFileName ) {   
          $this->saveFile( $staticFileName, $cacheContent );   
      }   
    
      if( $this->cacheLimitTime )   
        $this->saveFile( $this->cacheFileName, $cacheContent );   
    }   
  }   
     
  /*  
   * 清除快取檔案  
   * string $fileName 指定檔案名稱(含函數)或者all(全部)  
   * 返回:清除成功返回true,反之返回false  
   */  
  function clearCache( $fileName = "all" ) {   
    if( $fileName != "all" ) {   
      $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;   
      if( file_exists( $fileName ) ) {   
        return @unlink( $fileName );   
      }else return false;   
    }   
    if ( is_dir( $this->cacheRoot ) ) {   
      if ( $dir = @opendir( $this->cacheRoot ) ) {   
        while ( $file = @readdir( $dir ) ) {   
          $check = is_dir( $file );   
          if ( !$check )   
          @unlink( $this->cacheRoot . $file );   
        }   
        @closedir( $dir );   
        return true;   
      }else{   
        return false;   
      }   
    }else{   
      return false;   
    }   
  }   
    
  /*  
   * 根據當前動態檔案產生快取檔案名  
   */  
  function getCacheFileName() {   
    return  $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;   
  }   
    
  /*  
   * 快取檔案建立時間  
   * string $fileName   快取檔案名(含相對路徑)  
   * 返回:檔案產生時間秒數,檔案不存在返回0  
   */  
  function getFileCreateTime( $fileName ) {   
    if( ! trim($fileName) ) return 0;   
    
    if( file_exists( $fileName ) ) {    
      return intval(filemtime( $fileName ));   
    }else return 0;   
  }   
     
  /*  
   * 儲存檔案  
   * string $fileName  檔案名稱(含相對路徑)  
   * string $text      檔案內容  
   * 返回:成功返回ture,失敗返回false  
   */  
  function saveFile($fileName, $text) {   
    if( ! $fileName || ! $text ) return false;   
    
    if( $this->makeDir( dirname( $fileName ) ) ) {   
      if( $fp = fopen( $fileName, "w" ) ) {   
        if( @fwrite( $fp, $text ) ) {   
          fclose($fp);   
          return true;   
        }else {   
          fclose($fp);   
          return false;   
        }   
      }   
    }   
    return false;   
  }   
    
  /*  
   * 連續建目錄  
   * string $dir 目錄字元串  
   * int $mode   許可權數字  
   * 返回:順利建立或者全部已建返回true,其它方式返回false  
   */  
  function makeDir( $dir, $mode = "0777" ) {   
    if( ! $dir ) return 0;   
    $dir = str_replace( "\", "/", $dir );  
      
    $mdir = "";  
    foreach( explode( "/", $dir ) as $val ) {  
      $mdir .= $val."/";  
      if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;   
         
      if( ! file_exists( $mdir ) ) {   
        if(!@mkdir( $mdir, $mode )){   
         return false;   
        }   
      }   
    }   
    return true;   
  }   
}   
?> 

上面使用算是頁面緩衝了,每次訪問頁面的時候,都會先檢測相應的快取頁面面檔案是否存在,如果不存在,就串連資料庫,得到資料,顯示頁面並同時產生快取頁面面檔案,這樣下次訪問的時候分頁檔就發揮作用了。(模板引擎和網上常見的一些緩衝類通常有此功能)

後來給大家介紹一個Memcache緩衝了,算是記憶體緩衝了

代碼

 代碼如下 複製代碼

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)n";
$get_result = $memcache->get('key');
echo "Data from the cache:n";
var_dump($get_result);
?>

Memcached是高效能的,分布式的記憶體對象緩衝系統,用於在Live App中減少資料庫負載,提升訪問速度。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.