Rumah pembangunan bahagian belakang tutorial php 一个不错php文件缓存类文件_PHP教程

一个不错php文件缓存类文件_PHP教程

Jul 20, 2016 am 11:11 AM
php dokumen baik hati cache

本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用哦,有需要的朋友可参考参考。

 代码如下 复制代码

class Cache {
 /** 缓存目录 **/
 var $CacheDir        = './c';
 /** 缓存的文件 **/
 var $CacheFile        = '';
 /** 文件缓存时间(分钟) **/
 var $CacheTime        = 0;
 /** 文件是否已缓存 **/
 var $CacheFound        = False;
 /** 错误及调试信息 **/
 var $DebugMsg        = NULL;

 function Cache($CacheTime = 0) {
  $this->CacheTime    = $CacheTime;
 }

 private function Run() {
  /** 缓存时间大于0,检测缓存文件的修改时间,在缓存时间内为缓存文件名,超过缓存时间为False,
                小于等于0,返回false,并清理已缓存的文件
         **/
  Return $this->CacheTime ? $this->CheckCacheFile() : $this->CleanCacheFile();
 }
 function GetCache($VistUrl,$CacheFileType = 'html')
 {
  $this->SetCacheFile($VistUrl,$CacheFileType);

  $fileName=$this->CheckCacheFile();
  if($fileName)
  {
   $fp = fopen($fileName,"r");
   $content_= fread($fp, filesize($fileName));
   fclose($fp);
   return $content_;
  }
  else
  {
   return false;
  }
 }
 private function SetCacheFile($VistUrl,$CacheFileType = 'html') {
  if(empty($VistUrl)) {
   /** 默认为index.html **/
   $this->CacheFile = 'index';
  }else {
   /** 传递参数为$_POST时 **/
   $this->CacheFile = is_array($VistUrl) ? implode('.',$VistUrl) : $VistUrl;
  }
  $this->CacheFile = $this->CacheDir.'/'.md5($this->CacheFile);
  $this->CacheFile.= '.'.$CacheFileType;
 }

 function SetCacheTime($t = 60) {
  $this->CacheTime = $t;
 }

 private function CheckCacheFile() {
  if(!$this->CacheTime || !file_exists($this->CacheFile)) {Return False;}
  /** 比较文件的建立/修改日期和当前日期的时间差 **/
  $GetTime=(Time()-Filemtime($this->CacheFile))/(60*1);
  /** Filemtime函数有缓存,注意清理 **/
  Clearstatcache();
  $this->Debug('Time Limit '.($GetTime*60).'/'.($this->CacheTime*60).'');
  $this->CacheFound = $GetTime CacheTime ? $this->CacheFile : False;
  Return $this->CacheFound;
 }

 function SaveToCacheFile($VistUrl,$Content,$CacheFileType = 'html') {
  $this->SetCacheFile($VistUrl,$CacheFileType);
  if(!$this->CacheTime) {
   Return False;
  }
  /** 检测缓存目录是否存在 **/
  if(true === $this->CheckCacheDir()) {
   $CacheFile = $this->CacheFile;
   $CacheFile = str_replace('//','/',$CacheFile);
   $fp = @fopen($CacheFile,"wb");
   if(!$fp) {
    $this->Debug('Open File '.$CacheFile.' Fail');
   }else {
    if(@!fwrite($fp,$Content)){
     $this->Debug('Write '.$CacheFile.' Fail');
    }else {
     $this->Debug('Cached File');
    };
    @fclose($fp);
   }
  }else {
   /** 缓存目录不存在,或不能建立目录 **/
   $this->Debug('Cache Folder '.$this->CacheDir.' Not Found');
  }
 }

 private function CheckCacheDir() {
  if(file_exists($this->CacheDir)) { Return true; }
  /** 保存当前工作目录 **/
  $Location = getcwd();
  /** 把路径划分成单个目录 **/
  $Dir = split("/", $this->CacheDir);
  /** 循环建立目录 **/
  $CatchErr = True;
  for ($i=0; $i   if (!file_exists($Dir[$i])){
    /** 建立目录失败会返回False 返回建立最后一个目录的返回值 **/
    $CatchErr = @mkdir($Dir[$i],0777);
   }
   @chdir($Dir[$i]);
  }
  /** 建立完成后要切换到原目录 **/
  chdir($Location);
  if(!$CatchErr) {
   $this->Debug('Create Folder '.$this->CacheDir.' Fail');
  }
  Return $CatchErr;
 }

 private function CleanCacheFile() {
  if(file_exists($this->CacheFile)) {
   @chmod($this->CacheFile,777);
   @unlink($this->CacheFile);
  }
  /** 置没有缓存文件 **/
  $this->CacheFound = False;
  Return $this->CacheFound;
 }

 function Debug($msg='') {
  if(DEBUG) {
   $this->DebugMsg[] = '[Cache]'.$msg;
  }
 }

 function GetError() {
  Return empty($this->DebugMsg) ? '' : "
n".implode("
n",$this->DebugMsg);
 }
}/* end of class */


?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444619.htmlTechArticle本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用哦,有需要的朋友可参考参考。 代码如下...
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Tag artikel panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Dec 24, 2024 pm 04:42 PM

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian

Tarikh dan Masa CakePHP Tarikh dan Masa CakePHP Sep 10, 2024 pm 05:27 PM

Tarikh dan Masa CakePHP

Konfigurasi Projek CakePHP Konfigurasi Projek CakePHP Sep 10, 2024 pm 05:25 PM

Konfigurasi Projek CakePHP

Muat naik Fail CakePHP Muat naik Fail CakePHP Sep 10, 2024 pm 05:27 PM

Muat naik Fail CakePHP

Penghalaan CakePHP Penghalaan CakePHP Sep 10, 2024 pm 05:25 PM

Penghalaan CakePHP

Bincangkan CakePHP Bincangkan CakePHP Sep 10, 2024 pm 05:28 PM

Bincangkan CakePHP

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Dec 20, 2024 am 11:31 AM

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP

Panduan Ringkas CakePHP Panduan Ringkas CakePHP Sep 10, 2024 pm 05:27 PM

Panduan Ringkas CakePHP

See all articles