一个不错的PHP缓存类介绍

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

cache.php 代码如下:

01<? 
02/*
03用户需要事先定义的常量:
04_CachePath_        模板缓存路径
05_CacheEnable_        自动缓存机制是否开启,未定义或为空,表示关闭自动缓存机制
06_ReCacheTime_        自动重新缓存间隔时间,单位为秒,未定义或为空,表示关闭自动重新缓存
07*/ 
08   
09class 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?>

类的使用:

01<?php 
02    define("_CachePath_","./cache/"); 
03    define("_CacheEnable_","1"); 
04    define("_ReCacheTime_","43200"); 
05    include('cache.php'); 
06    $cache=new cache(); 
07    if ($cache->check())
08    
09        $template=$cache->read(); 
10    }
11    else
12    
13        ob_start(); 
14        ob_implicit_flush(0); 
15?> 
16    页面内容。。。。 
17<?php 
18        $template = ob_get_contents(); 
19        $cache->write($template); 
20    
21?> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值