本人給大家推一個不錯php文件緩存類文件,從各方面來看本緩存類很合理并且適用于大型網站使用,php文件緩存類文件代碼如下:
- <?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(emptyempty($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 <= $this->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<count($Dir); $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 emptyempty($this->DebugMsg) ? '' : "<br>n".implode("<br>n",$this->DebugMsg);//開源代碼Vevb.com
- }
- }/* end of class */
- ?>
新聞熱點
疑難解答