[PHP] PHP缓存类 Cache (文件实现) →→→→→进入此内容的聊天室

来自 , 2020-10-29, 写在 PHP, 查看 138 次.
URL http://www.code666.cn/view/a92c274b
  1. <?  
  2. /*
  3. 用户需要事先定义的常量:
  4. _CachePath_        模板缓存路径
  5. _CacheEnable_        自动缓存机制是否开启,未定义或为空,表示关闭自动缓存机制
  6. _ReCacheTime_        自动重新缓存间隔时间,单位为秒,未定义或为空,表示关闭自动重新缓存
  7. */  
  8.  
  9. class cache
  10. {
  11.         var $cachefile;  
  12.         var $cachefilevar;  
  13.  
  14.         function cache()
  15.         {  
  16.         //生成当前页的Cache组文件名 $this->cachefilevar 及文件名 $this->cachefile  
  17.         //动态页的参数不同对应的Cache文件也不同,但是每一个动态页的所有Cache文件都有相同的文件名,只是扩展名不同  
  18.         $s=array(".","/");$r=array("_","");  
  19.         $this->cachefilevar=str_replace($s,$r,$_SERVER["SCRIPT_NAME"])."_".$_GET[_ActionVar_];  
  20.         $this->cachefile=$this->cachefilevar.".".md5($_SERVER["REQUEST_URI"]);  
  21.         }  
  22.  
  23.         //删除当前页/模块的缓存  
  24.         function delete()
  25.         {  
  26.         //删除当前页的缓存  
  27.         $d = dir(_CachePath_);  
  28.         $strlen=strlen($this->cachefilevar);  
  29.         //返回当前页的所有Cache文件组  
  30.         while (false !== ($entry = $d->read()))
  31.                 {  
  32.                 if (substr($entry,0,$strlen)==$this->cachefilevar)
  33.                         {  
  34.                         if (!unlink(_CachePath_."/".$entry)) {echo "Cache目录无法写入";exit;}  
  35.                 }  
  36.         }  
  37.         }  
  38.  
  39.         //判断是否已Cache过,以及是否需要Cache  
  40.         function check()
  41.         {  
  42.         //如果设置了缓存更新间隔时间 _ReCacheTime_  
  43.         if (_ReCacheTime_+0>0)
  44.                 {  
  45.                 //返回当前页Cache的最后更新时间  
  46.                 $var=@file(_CachePath_."/".$this->cachefilevar);$var=$var[0];  
  47.                 //如果更新时间超出更新间隔时间则删除Cache文件  
  48.                 if (time()-$var>_ReCacheTime_)
  49.                         {  
  50.                         $this->delete();$ischage=true;  
  51.                 }  
  52.                 }  
  53.         //返回当前页的Cache  
  54.         $file=_CachePath_."/".$this->cachefile;  
  55.         //判断当前页Cache是否存在 且 Cache功能是否开启  
  56.         return (file_exists($file) and _CacheEnable_ and !$ischange);  
  57.         }  
  58.  
  59.         //读取Cache  
  60.         function read()
  61.         {  
  62.         //返回当前页的Cache  
  63.         $file=_CachePath_."/".$this->cachefile;  
  64.         //读取Cache文件的内容  
  65.         if (_CacheEnable_) return readfile($file);  
  66.         else return false;  
  67.         }  
  68.  
  69.         //生成Cache  
  70.         function write($output)
  71.         {  
  72.         //返回当前页的Cache  
  73.         $file=_CachePath_."/".$this->cachefile;  
  74.         //如果Cache功能开启  
  75.         if (_CacheEnable_)
  76.                 {  
  77.                 //把输出的内容写入Cache文件  
  78.                 $fp=@fopen($file,'w');  
  79.                 if (!@fputs($fp,$output)) {echo "模板Cache写入失败";exit;}  
  80.                 @fclose($fp);  
  81.                 //如果设置了缓存更新间隔时间 _ReCacheTime_  
  82.                 if (_ReCacheTime_+0>0)
  83.                         {  
  84.                 //更新当前页Cache的最后更新时间  
  85.                 $file=_CachePath_."/".$this->cachefilevar;  
  86.                 $fp=@fopen($file,'w');  
  87.                 if (!@fwrite($fp,time())) {echo "Cache目录无法写入";exit;}  
  88.                 @fclose($fp);  
  89.                 }  
  90.                 }  
  91.         }  
  92. }  
  93. ?>
  94.  

回复 "PHP缓存类 Cache (文件实现)"

这儿你可以回复上面这条便签

captcha