1 |
- <?php
require('set.php');
if(CACHE_CLOSE) return;
date_default_timezone_set("Asia/Shanghai");
$CacheName=md5($_SERVER['REQUEST_URI']) . CACHE_FIX;
$CacheDir=CACHE_ROOT . DIRECTORY_SEPARATOR . substr($CacheName,0,1);
$CacheUrl=$CacheDir . DIRECTORY_SEPARATOR . $CacheName;
if($_SERVER['REQUEST_METHOD']=='GET'){
if(file_exists($CacheUrl) && time()-filemtime($CacheUrl)<CACHE_TIME){
echo gzuncompress(file_get_contents($CacheUrl));
exit;
}
elseif(!file_exists($CacheDir)){
if(!file_exists(CACHE_ROOT)){
mkdir(CACHE_ROOT,0777);
chmod(CACHE_ROOT,0777);
}
mkdir($CacheDir,0777);
chmod($CacheDir,0777);
}
function AutoCache($contents){
global $CacheUrl;
$fp=fopen($CacheUrl,'wb');
$contents =$contents;
fwrite($fp,gzcompress($contents));
fclose($fp);
chmod($CacheUrl,0777);
DelOldCache();
return $contents;
}
function DelOldCache(){
chdir(CACHE_ROOT);
foreach (glob("*/*".CACHE_FIX) as $file){
if(time()-filemtime($file)>CACHE_TIME) unlink($file);
}
}
ob_start('AutoCache');
clearstatcache();
}else{
if(file_exists($CacheUrl)) unlink($CacheUrl);
}
?>
|