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 */
?>
|