缓存在实际使用当中应用很广泛,可以减轻对服务器数据库的访问,提高运行速度。目前很多CMS内容管理系统中频繁使用缓存机制来提高系统运行的效率。下面是一个写得不错的缓存类,可以参考下缓存的机制与写法。
cache.php 代码如下:
01 | <? |
02 | /* |
03 | 用户需要事先定义的常量: |
04 | _CachePath_ 模板缓存路径 |
05 | _CacheEnable_ 自动缓存机制是否开启,未定义或为空,表示关闭自动缓存机制 |
06 | _ReCacheTime_ 自动重新缓存间隔时间,单位为秒,未定义或为空,表示关闭自动重新缓存 |
07 | */ |
08 | |
09 | 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 | ?> |
类的使用:
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 | ?> |