国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP文件緩存類實現(xiàn)代碼

2024-05-04 23:39:51
字體:
供稿:網(wǎng)友
這篇文章主要介紹了PHP文件緩存類實現(xiàn)代碼,php中緩存分類數(shù)據(jù)庫緩存,文件緩存和內(nèi)存緩存,對php緩存感興趣的朋友可以學習學習下面的文章。
 

php中緩存分類數(shù)據(jù)庫緩存,文件緩存內(nèi)存緩存,下面我來給各位同學詳細介紹PHP文件緩存類實現(xiàn)代碼,有需要了解的朋友可參考。
頁面緩存類
代碼如下 :
 

  1. <?php   
  2. /*include( "cache.php" );   
  3.    
  4. $cache = new cache(30);   
  5. $cache->cacheCheck();   
  6.    
  7. echo date("Y-m-d H:i:s");   
  8.    
  9. $cache->caching(); */ 
  10. class cache {   
  11.  //緩存目錄   
  12.  var $cacheRoot    = "./cache/";   
  13.  //緩存更新時間秒數(shù),0為不緩存   
  14.  var $cacheLimitTime  = 3;  
  15.  //緩存文件名   
  16.  var $cacheFileName  = "";   
  17.  //緩存擴展名   
  18.  var $cacheFileExt   = "php";   
  19.     
  20.  /*   
  21.   * 構造函數(shù)   
  22.   * int $cacheLimitTime 緩存更新時間   
  23.   */ 
  24.  function cache( $cacheLimitTime ) {   
  25.   if( intval( $cacheLimitTime ) )    
  26.    $this->cacheLimitTime = $cacheLimitTime;   
  27.   $this->cacheFileName = $this->getCacheFileName();   
  28.   ob_start();   
  29.  }   
  30.     
  31.  /*   
  32.   * 檢查緩存文件是否在設置更新時間之內(nèi)   
  33.   * 返回:如果在更新時間之內(nèi)則返回文件內(nèi)容,反之則返回失敗   
  34.   */ 
  35.  function cacheCheck(){   
  36.   if( file_exists( $this->cacheFileName ) ) {   
  37.    $cTime = $this->getFileCreateTime( $this->cacheFileName );   
  38.    if( $cTime + $this->cacheLimitTime > time() ) {   
  39.     echo file_get_contents( $this->cacheFileName );   
  40.     ob_end_flush();   
  41.     exit;   
  42.    }   
  43.   }   
  44.   return false;   
  45.  }   
  46.     
  47.  /*   
  48.   * 緩存文件或者輸出靜態(tài)   
  49.   * string $staticFileName 靜態(tài)文件名(含相對路徑)   
  50.   */ 
  51.  function caching( $staticFileName = "" ){   
  52.   if( $this->cacheFileName ) {   
  53.    $cacheContent = ob_get_contents();   
  54.    //echo $cacheContent;   
  55.    ob_end_flush();   
  56.     
  57.    if( $staticFileName ) {   
  58.      $this->saveFile( $staticFileName, $cacheContent );   
  59.    }   
  60.     
  61.    if( $this->cacheLimitTime )   
  62.     $this->saveFile( $this->cacheFileName, $cacheContent );   
  63.   }   
  64.  }   
  65.     
  66.  /*   
  67.   * 清除緩存文件   
  68.   * string $fileName 指定文件名(含函數(shù))或者all(全部)   
  69.   * 返回:清除成功返回true,反之返回false   
  70.   */ 
  71.  function clearCache( $fileName = "all" ) {   
  72.   if( $fileName != "all" ) {   
  73.    $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;   
  74.    if( file_exists( $fileName ) ) {   
  75.     return @unlink( $fileName );   
  76.    }else return false;   
  77.   }   
  78.   if ( is_dir( $this->cacheRoot ) ) {   
  79.    if ( $dir = @opendir( $this->cacheRoot ) ) {   
  80.     while ( $file = @readdir( $dir ) ) {   
  81.      $check = is_dir( $file );   
  82.      if ( !$check )   
  83.      @unlink( $this->cacheRoot . $file );   
  84.     }   
  85.     @closedir( $dir );   
  86.     return true;   
  87.    }else{   
  88.     return false;   
  89.    }   
  90.   }else{   
  91.    return false;   
  92.   }   
  93.  }   
  94.     
  95.  /*   
  96.   * 根據(jù)當前動態(tài)文件生成緩存文件名   
  97.   */ 
  98.  function getCacheFileName() {   
  99.   return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;   
  100.  }   
  101.     
  102.  /*   
  103.   * 緩存文件建立時間   
  104.   * string $fileName  緩存文件名(含相對路徑)   
  105.   * 返回:文件生成時間秒數(shù),文件不存在返回0   
  106.   */ 
  107.  function getFileCreateTime( $fileName ) {   
  108.   if( ! trim($fileName) ) return 0;   
  109.     
  110.   if( file_exists( $fileName ) ) {    
  111.    return intval(filemtime( $fileName ));   
  112.   }else return 0;   
  113.  }   
  114.     
  115.  /*   
  116.   * 保存文件   
  117.   * string $fileName 文件名(含相對路徑)   
  118.   * string $text   文件內(nèi)容   
  119.   * 返回:成功返回ture,失敗返回false   
  120.   */ 
  121.  function saveFile($fileName, $text) {   
  122.   if( ! $fileName || ! $text ) return false;   
  123.     
  124.   if( $this->makeDir( dirname( $fileName ) ) ) {   
  125.    if( $fp = fopen( $fileName, "w" ) ) {   
  126.     if( @fwrite( $fp, $text ) ) {   
  127.      fclose($fp);   
  128.      return true;   
  129.     }else {   
  130.      fclose($fp);   
  131.      return false;   
  132.     }   
  133.    }   
  134.   }   
  135.   return false;   
  136.  }   
  137.     
  138.  /*   
  139.   * 連續(xù)建目錄   
  140.   * string $dir 目錄字符串   
  141.   * int $mode  權限數(shù)字   
  142.   * 返回:順利創(chuàng)建或者全部已建返回true,其它方式返回false   
  143.   */ 
  144.  function makeDir( $dir, $mode = "0777" ) {   
  145.   if( ! $dir ) return 0;   
  146.   $dir = str_replace( """/", $dir );   
  147.      
  148.   $mdir = "";   
  149.   foreach( explode( "/", $dir ) as $val ) {   
  150.    $mdir .= $val."/";   
  151.    if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;   
  152.       
  153.    if( ! file_exists( $mdir ) ) {   
  154.     if(!@mkdir( $mdir, $mode )){   
  155.      return false;   
  156.     }   
  157.    }   
  158.   }   
  159.   return true;   
  160.  }   
  161. }   
  162. ?>  
?
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 拉萨市| 铜山县| 吴川市| 黄浦区| 台安县| 林州市| 彰化县| 广灵县| 扶风县| 边坝县| 通辽市| 南川市| 林西县| 高州市| 浦城县| 内乡县| 新乡县| 昌图县| 安泽县| 屏东市| 灵石县| 金平| 赫章县| 汝州市| 伊宁县| 平南县| 安国市| 安徽省| 金湖县| 古蔺县| 锡林郭勒盟| 若尔盖县| 大庆市| 阿克陶县| 阿克苏市| 富蕴县| 白山市| 玉溪市| 连江县| 广南县| 惠水县|