当前位置:Gxlcms > PHP教程 > 网站适用的PHP缓存类_PHP教程

网站适用的PHP缓存类_PHP教程

时间:2021-07-01 10:21:17 帮助过:4人阅读

缓存在实际使用当中应用很广泛,可以减轻对服务器数据库的访问,提高运行速度。目前很多CMS内容管理系统中频繁使用缓存机制来提高系统运行的效率。下面是一个写得不错的缓存类,可以参考下缓存的机制与写法。

cache.php 代码如下:

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


类的使用:

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. define("_CachePath_","./cache/");
  2. define("_CacheEnable_","1");
  3. define("_ReCacheTime_","43200");
  4. include('cache.php');
  5. $cache=new cache();
  6. if ($cache->check())
  7. {
  8. $template=$cache->read();
  9. }
  10. else
  11. {
  12. ob_start();
  13. ob_implicit_flush(0);
  14. ?>
  15. 页面内容。。。。
  16. $template = ob_get_contents();
  17. $cache->write($template);
  18. }
  19. ?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/750358.htmlTechArticle缓存在实际使用当中应用很广泛,可以减轻对服务器数据库的访问,提高运行速度。目前很多CMS内容管理系统中频繁使用缓存机制来提高系...

人气教程排行