search
Homephp教程php手册网站适用的PHP缓存类

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

cache.php 代码如下:

[php] view plaincopy  
  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. ?>  


 

 

类的使用:

[php] view plaincopy  
  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. ?>    


 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor