数码鹭岛论坛

标题: 一个实用的PHP缓存类 [打印本页]

作者: 翔子    时间: 2009-1-5 16:50
标题: 一个实用的PHP缓存类
cache.php 代码如下:
  1. <?
  2. /*
  3. 用户需要事先定义的常量:
  4. _CachePath_        模板缓存路径
  5. _CacheEnable_        自动缓存机制是否开启,未定义或为空,表示关闭自动缓存机制
  6. _ReCacheTime_        自动重新缓存间隔时间,单位为秒,未定义或为空,表示关闭自动重新缓存
  7. */

  8. class cache {

  9. var $cachefile;
  10. var $cachefilevar;

  11. function cache() {
  12.         //生成当前页的Cache组文件名 $this->cachefilevar 及文件名 $this->cachefile
  13.         //动态页的参数不同对应的Cache文件也不同,但是每一个动态页的所有Cache文件都有相同的文件名,只是扩展名不同
  14.         $s=array(".","/");$r=array("_","");
  15.         $this->cachefilevar=str_replace($s,$r,$_SERVER["SCRIPT_NAME"])."_".$_GET[_ActionVar_];
  16.         $this->cachefile=$this->cachefilevar.".".md5($_SERVER["REQUEST_URI"]);
  17. }

  18. //删除当前页/模块的缓存
  19. function delete() {
  20.         //删除当前页的缓存
  21.         $d = dir(_CachePath_);
  22.         $strlen=strlen($this->cachefilevar);
  23.         //返回当前页的所有Cache文件组
  24.         while (false !== ($entry = $d->read())) {
  25.                     if (substr($entry,0,$strlen)==$this->cachefilevar) {
  26.                             if (!unlink(_CachePath_."/".$entry)) {echo "Cache目录无法写入";exit;}
  27.                     }
  28.             }
  29. }

  30. //判断是否已Cache过,以及是否需要Cache
  31. function check() {
  32.         //如果设置了缓存更新间隔时间 _ReCacheTime_
  33.         if (_ReCacheTime_+0>0)        {
  34.                 //返回当前页Cache的最后更新时间
  35.                 $var=@file(_CachePath_."/".$this->cachefilevar);$var=$var[0];
  36.                 //如果更新时间超出更新间隔时间则删除Cache文件
  37.                 if (time()-$var>_ReCacheTime_) {
  38.                         $this->delete();$ischage=true;
  39.                 }
  40.         }
  41.         //返回当前页的Cache
  42.         $file=_CachePath_."/".$this->cachefile;
  43.         //判断当前页Cache是否存在 且 Cache功能是否开启
  44.         return (file_exists($file) and _CacheEnable_ and !$ischange);
  45. }

  46. //读取Cache
  47. function read() {
  48.         //返回当前页的Cache
  49.         $file=_CachePath_."/".$this->cachefile;
  50.         //读取Cache文件的内容
  51.         if (_CacheEnable_) return readfile($file);
  52.         else return false;
  53. }

  54. //生成Cache
  55. function write($output) {
  56.         //返回当前页的Cache
  57.         $file=_CachePath_."/".$this->cachefile;
  58.         //如果Cache功能开启
  59.         if (_CacheEnable_) {
  60.                 //把输出的内容写入Cache文件
  61.                 $fp=@fopen($file,'w');
  62.                 if (!@fputs($fp,$output)) {echo "模板Cache写入失败";exit;}
  63.                 @fclose($fp);
  64.                 //如果设置了缓存更新间隔时间 _ReCacheTime_
  65.                 if (_ReCacheTime_+0>0) {
  66.                         //更新当前页Cache的最后更新时间
  67.                         $file=_CachePath_."/".$this->cachefilevar;
  68.                         $fp=@fopen($file,'w');
  69.                         if (!@fwrite($fp,time())) {echo "Cache目录无法写入";exit;}
  70.                         @fclose($fp);
  71.                 }
  72.         }
  73. }

  74. }
  75. ?>
复制代码





欢迎光临 数码鹭岛论坛 (http://www.clore.net/forum/) Powered by Discuz! X3.2