1 <?php
2 /*
3 * 自定义页面缓存类
4 */
5 namespace page_cache;
6 class Page
7 {
8 public $CacheRoot = "pageCache/";//缓存文件存放目录,
9 public $CacheLimitTime = 0;//缓存文件更新时间,0不更新
10 public $CacheFileName = '';//缓存文件名,
11 public $CacheFileExt = '.php';//缓存文件扩展名称
12
13 //构造函数,设置缓存更新时间
14 public function __construct($CacheLimitTime)
15 {
16 if(intval($CacheLimitTime))
17 {
18 $this->CacheLimitTime = $CacheLimitTime;
19 $this->CacheFileName = $this->GetFileName();
20 ob_start();
21 return true;
22 }
23 else
24 {
25 return false;
26 }
27 }
28
29
30 //检查缓存文件是否在更新时间内,在更新时间内返回文件内容,不在更新时间内返回false
31 public function CheckTime()
32 {
33 if(file_exists($this->CacheFileName))
34 {
35 if($this->CacheLimitTime +$this->GetFileCreateTime($this->CacheFileName)> time())
36 {
37 echo file_get_contents($this->CacheFileName);
38 ob_end_flush();//输出内容到缓冲区
39 exit();
40 }
41 }
42 return false;
43 }
44
45 /*
46 * 创建缓存文件
47 * param $cacheFileName; type string; value 自定义缓存文件的名称
48 */
49 public function CreateCacheFile($cacheFileName='')
50 {
51 $getCacheContent = ob_get_contents();
52 ob_end_flush ();
53 if(!empty($cacheFileName))
54 {
55 return $this->SaveFile($cacheFileName,$getCacheContent);
56 }
57 else
58 {
59 return $this->SaveFile($this->CacheFileName,$getCacheContent);
60 }
61 }
62
63 /*
64 * 清除缓存
65 * param $fileName;type string;value all;note:value is all,clear all cache;
66 */
67 public function ClearCache($fileName = "all")
68 {
69 if($fileName!='all')
70 {
71 $fileName = $this->CacheRoot.strtoupper(md5($fileName)).$this->CacheFileExt;
72 if(file_exists($fileName))
73 {
74 return @unlink($fileName);
75 }
76 else
77 {
78 return false;
79 }
80 }
81 if($fileName = 'all')
82 {
83 if(is_dir(($this->CacheRoot)))
84 {
85 if($dir = opendir($this->CacheRoot))
86 {
87 while($file = readdir($dir))
88 {
89 if(!is_dir($file))
90 {
91 unlink($this->CacheRoot.$file);
92 }
93
94 }
95 closedir($dir);
96 return true;
97 }
98 }
99 else
100 {
101 return false;
102 }
103 }
104 }
105 //获取缓存的文件名称
106 public function GetFileName()
107 {
108 return $this->CacheRoot.strtoupper(md5($_SERVER['REQUEST_URI'])).$this->CacheFileExt;
109 }
110
111 //返回缓存文件上次修改的时间
112 public function GetFileCreateTime($filename)
113 {
114 if(!trim($filename)) return 0;
115 if(file_exists($filename))
116 {
117 return intval(filemtime($filename));
118 }
119 else
120 {
121 return 0;
122 }
123 }
124
125 //保存文件并写入内容
126 public function SaveFile($filename,$text)
127 {
128 if(empty($filename)||empty($text))
129 {
130 return false;
131 }
132 if($this->CacheDir(dirname($filename)))
133 {
134 if($fp = fopen($filename,"w"))
135 {
136 if(fwrite($fp,$text))
137 {
138 fclose($fp);
139 return true;
140 }
141 else
142 {
143 fclose($fp);
144 return false;
145 }
146 }
147 }
148
149 }
150
151
152 //创建缓存目录
153 public function CacheDir($dir,$mode="0777")
154 {
155 if(empty($dir))
156 {
157 return false;
158 }
159 $dir = str_replace("\\","/",$dir);
160
161 $dir = explode("/",$dir);
162
163 $mkdir = '';
164 foreach($dir as $val)
165 {
166 $mkdir .= $val.'/';
167 if($val=='./'||$val=='../'||$val=="")
168 {
169 continue;
170 }
171 if(!file_exists($mkdir))
172 {
173 if(!@mkdir($mkdir,$mode))
174 {
175 return false;
176 }
177 }
178 }
179
180 return true;
181
182 }
183
184 }
185
186
187 $cache = new Page(30);
188 $cache->CheckTime();
189 echo "Now is : " . date ( "Y-m-d H:i:s" ,time());
190 $cache ->CreateCacheFile();