设为首页收藏本站

数码鹭岛论坛

 找回密码
 注-册

QQ登录

只需一步,快速开始

搜索
查看: 5237|回复: 0
打印 上一主题 下一主题

一个实用的PHP缓存类

[复制链接]
跳转到指定楼层
1#
发表于 2009-1-5 16:50:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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. ?>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 注-册

本版积分规则

小黑屋|手机版|Archiver|数码鹭岛 ( 闽ICP备20006246号 )  

counter

GMT+8, 2025-12-4 02:10 , Processed in 0.065719 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表