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();