PHP 캐시 클래스 코드 1개(상세 설명 첨부)

1755 단어
 
  
define('CACHE_ROOT', dirname(__FILE__).'/cache'); //
define('CACHE_TIME', 1800);//
define('CACHE_FIX','.html');
$CacheName=md5($_SERVER['REQUEST_URI']).CACHE_FIX; //
$CacheDir=CACHE_ROOT.'/'.substr($CacheName,0,1);//
$CacheUrl=$CacheDir.'/'.$CacheName;//
//GET ,POST
if($_SERVER['REQUEST_METHOD']=='GET'){
// , , 。
if(file_exists($CacheName) && time()-filemtime($CacheName)$fp=fopen($CacheName,'rb');
fpassthru($fp);
fclose($fp);
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');
fwrite($fp,$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);
}
}
// auto_cache
ob_start('AutoCache');
}else{
// GET 。
if(file_exists($CacheUrl))unlink($CacheUrl);
}
?>

좋은 웹페이지 즐겨찾기