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

首頁 > 語言 > PHP > 正文

php文件緩存類文件

2024-09-04 11:44:08
字體:
來源:轉載
供稿:網友

本人給大家推一個不錯php文件緩存類文件,從各方面來看本緩存類很合理并且適用于大型網站使用,php文件緩存類文件代碼如下:

  1. <?php 
  2. class Cache { 
  3.  /** 緩存目錄 **/ 
  4.  var $CacheDir        = './c'
  5.  /** 緩存的文件 **/ 
  6.  var $CacheFile        = ''
  7.  /** 文件緩存時間(分鐘) **/ 
  8.  var $CacheTime        = 0; 
  9.  /** 文件是否已緩存 **/ 
  10.  var $CacheFound        = False; 
  11.  /** 錯誤及調試信息 **/ 
  12.  var $DebugMsg        = NULL; 
  13.  
  14.  function Cache($CacheTime = 0) { 
  15.   $this->CacheTime    = $CacheTime
  16.  } 
  17.  
  18.  private function Run() { 
  19.   /** 緩存時間大于0,檢測緩存文件的修改時間,在緩存時間內為緩存文件名,超過緩存時間為False, 
  20.                 小于等于0,返回false,并清理已緩存的文件 
  21.          **/ 
  22.   Return $this->CacheTime ? $this->CheckCacheFile() : $this->CleanCacheFile(); 
  23.  } 
  24.  function GetCache($VistUrl,$CacheFileType = 'html'
  25.  { 
  26.   $this->SetCacheFile($VistUrl,$CacheFileType); 
  27.  
  28.   $fileName=$this->CheckCacheFile(); 
  29.   if($fileName
  30.   { 
  31.    $fp = fopen($fileName,"r"); 
  32.    $content_fread($fpfilesize($fileName)); 
  33.    fclose($fp); 
  34.    return $content_
  35.   } 
  36.   else 
  37.   { 
  38.    return false; 
  39.   } 
  40.  } 
  41.  private function SetCacheFile($VistUrl,$CacheFileType = 'html') { 
  42.   if(emptyempty($VistUrl)) { 
  43.    /** 默認為index.html **/ 
  44.    $this->CacheFile = 'index'
  45.   }else { 
  46.    /** 傳遞參數為$_POST時 **/ 
  47.    $this->CacheFile = is_array($VistUrl) ? implode('.',$VistUrl) : $VistUrl
  48.   } 
  49.   $this->CacheFile = $this->CacheDir.'/'.md5($this->CacheFile); 
  50.   $this->CacheFile.= '.'.$CacheFileType
  51.  } 
  52.  
  53.  function SetCacheTime($t = 60) { 
  54.   $this->CacheTime = $t
  55.  } 
  56.  
  57.  private function CheckCacheFile() { 
  58.   if(!$this->CacheTime || !file_exists($this->CacheFile)) {Return False;} 
  59.   /** 比較文件的建立/修改日期和當前日期的時間差 **/ 
  60.   $GetTime=(Time()-Filemtime($this->CacheFile))/(60*1); 
  61.   /** Filemtime函數有緩存,注意清理 **/ 
  62.   Clearstatcache(); 
  63.   $this->Debug('Time Limit '.($GetTime*60).'/'.($this->CacheTime*60).''); 
  64.   $this->CacheFound = $GetTime <= $this->CacheTime ? $this->CacheFile : False; 
  65.   Return $this->CacheFound; 
  66.  } 
  67.  
  68.  function SaveToCacheFile($VistUrl,$Content,$CacheFileType = 'html') { 
  69.   $this->SetCacheFile($VistUrl,$CacheFileType); 
  70.   if(!$this->CacheTime) { 
  71.    Return False; 
  72.   } 
  73.   /** 檢測緩存目錄是否存在 **/ 
  74.   if(true === $this->CheckCacheDir()) { 
  75.    $CacheFile = $this->CacheFile; 
  76.    $CacheFile = str_replace('//','/',$CacheFile); 
  77.    $fp = @fopen($CacheFile,"wb"); 
  78.    if(!$fp) { 
  79.     $this->Debug('Open File '.$CacheFile.' Fail'); 
  80.    }else { 
  81.     if(@!fwrite($fp,$Content)){ 
  82.      $this->Debug('Write '.$CacheFile.' Fail'); 
  83.     }else { 
  84.      $this->Debug('Cached File'); 
  85.     }; 
  86.     @fclose($fp); 
  87.    } 
  88.   }else { 
  89.    /** 緩存目錄不存在,或不能建立目錄 **/ 
  90.    $this->Debug('Cache Folder '.$this->CacheDir.' Not Found'); 
  91.   } 
  92.  } 
  93.  
  94.  private function CheckCacheDir() { 
  95.   if(file_exists($this->CacheDir)) { Return true; } 
  96.   /** 保存當前工作目錄 **/ 
  97.   $Location = getcwd(); 
  98.   /** 把路徑劃分成單個目錄 **/ 
  99.   $Dir = split("/"$this->CacheDir); 
  100.   /** 循環建立目錄 **/ 
  101.   $CatchErr = True; 
  102.   for ($i=0; $i<count($Dir); $i++){ 
  103.    if (!file_exists($Dir[$i])){ 
  104.     /** 建立目錄失敗會返回False 返回建立最后一個目錄的返回值 **/ 
  105.     $CatchErr = @mkdir($Dir[$i],0777); 
  106.    } 
  107.    @chdir($Dir[$i]); 
  108.   } 
  109.   /** 建立完成后要切換到原目錄 **/ 
  110.   chdir($Location); 
  111.   if(!$CatchErr) { 
  112.    $this->Debug('Create Folder '.$this->CacheDir.' Fail'); 
  113.   } 
  114.   Return $CatchErr
  115.  } 
  116.  
  117.  private function CleanCacheFile() { 
  118.   if(file_exists($this->CacheFile)) { 
  119.    @chmod($this->CacheFile,777); 
  120.    @unlink($this->CacheFile); 
  121.   } 
  122.   /** 置沒有緩存文件 **/ 
  123.   $this->CacheFound = False; 
  124.   Return $this->CacheFound; 
  125.  } 
  126.  
  127.  function Debug($msg='') { 
  128.   if(DEBUG) { 
  129.    $this->DebugMsg[] = '[Cache]'.$msg
  130.   } 
  131.  } 
  132.  
  133.  function GetError() { 
  134.   Return emptyempty($this->DebugMsg) ? '' : "<br>n".implode("<br>n",$this->DebugMsg);//開源代碼Vevb.com 
  135.  } 
  136. }/* end of class */ 
  137. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅江市| 南部县| 商南县| 雷山县| 平昌县| 河西区| 蛟河市| 新宾| 阳信县| 宿州市| 嘉义市| 崇明县| 鲁山县| 鄢陵县| 林周县| 舟曲县| 葵青区| 嘉定区| 兴化市| 周至县| 双桥区| 葫芦岛市| 师宗县| 炎陵县| 隆化县| 滨州市| 杭锦旗| 淮南市| 绥滨县| 普定县| 丁青县| 新田县| 肇源县| 巴里| 华亭县| 晴隆县| 汕头市| 佛山市| 鲁甸县| 红河县| 丹江口市|