Home > Backend Development > PHP Tutorial > A good php file cache class file_PHP tutorial

A good php file cache class file_PHP tutorial

WBOY
Release: 2016-07-20 11:11:46
Original
821 people have browsed it

I recommend a good PHP file cache class file to everyone. From all aspects, this cache class is very reasonable and suitable for large websites. Friends in need can refer to it.

The code is as follows Copy code

class Cache {
/** Cache directory **/
var $CacheDir = './c';
/**Cached files **/
var $CacheFile = '';
/**File cache time (minutes) **/
var $CacheTime = 0;
/**Whether the file is cached **/
var $CacheFound = False;
/**Error and debugging information **/
var $DebugMsg = NULL;

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

private function Run() {
/**If the cache time is greater than 0, detect the modification time of the cached file. It is the cached file name within the cache time. If it exceeds the cache time, it is 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)) {
/**Default is index.html **/
$this->CacheFile = 'index';
}else {
/**When the passed parameter is $_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;}

/**Compare the time difference between the file's creation/modification date and the current date **/
$GetTime=(Time()-Filemtime($this->CacheFile))/(60*1) ;
/**Filemtime function has cache, please pay attention to clean it **/
Clearstatcache();
$this->Debug('Time Limit '.($GetTime*60).'/'.($this-> ;CacheTime*60).'');
$this->CacheFound = $GetTime <= $this->CacheTime ? $this->CacheFile : False;
Return $this-> CacheFound;
}

 function SaveToCacheFile($VistUrl,$Content,$CacheFileType = 'html') {
  $this->SetCacheFile($VistUrl,$CacheFileType);
  if(!$this->CacheTime) {
   Return False;
  }
  /**Check whether the cache directory exists **/
  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 {
   /**The cache directory does not exist, or the directory cannot be created **/
   $this->Debug('Cache Folder '.$this->CacheDir.' Not Found');
  }
 }

 private function CheckCacheDir() {
  if(file_exists($this->CacheDir)) { Return true; }
  /**Save current working directory **/
  $Location = getcwd();
  /**Divide the path into individual directories **/
  $Dir = split("/", $this->CacheDir);
  /**Loop to create directories **/
  $CatchErr = True;
  for ($i=0; $i   if (!file_exists($Dir[$i])){
    /**If the directory creation fails, False will be returned and the return value of the last directory created will be returned **/
    $CatchErr = @mkdir($Dir[$i],0777);
   }
   @chdir($Dir[$i]);
  }
  /**After the creation is completed, switch to the original directory **/
  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);
  }
  /**Set no cache files **/
  $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文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用哦,有需要的朋友可参考参考。 代码如下...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template