프로그래밍/PHP

[기타] cron을 사용하지않고 화일 캐쉬

백룡화검 2010. 4. 23. 21:28

 DB 억세스가 많은 인덱스화일을 html로 저장했다가 보여주는 방식의 인덱스화일 캐쉬는 팁텍에서도 여러번 거론 되었던 방법이고 스쿨에서도 이미 사용하고 있는 방법입니다만...

비록 뒷북일지라도...

--------------------------------------------------------------------------------------------------------

function make_cache($file,$script,$duration=60) {
    $limit=time()-$duration*60;
    if(!file_exists($file) || filemtime($file)<$limit) {
        $command="wget --user-agent=\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) \" http://".$_SERVER["HTTP_HOST"].$script." -O ".$file;
        exec($command);
    }
    if(file_exists($file))  @include $file;
}

function new_cache($file,$script) {
        $command="wget --user-agent=\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) \" http://".$_SERVER["HTTP_HOST"].$script." -O ".$file;
        exec($command);
}

--------------------------------------------------------------------------------------------------------

크론을 돌리지 않고도 인덱스화일을 통채로 저장하는 것보다는 몇조각으로 나눠서 저장하는 방법을 사용하고 있습니다.

사용법은
<? include "head.php"; ?>
<tr>
<td width=545 valign=top>
<table valign=top cellpadding=0 cellspacing=0 border=0 width=100%>
<?
$inc=$_SERVER[DOCUMENT_ROOT]."/layout/cache/center.html";
$script="/layout/center.inc" ;
make_cache($inc,$script,5);
?>
</table></td>
<td width=8></td>
<td width=245 valign=top><table valign=top cellpadding=0 cellspacing=0 border=0>
<?
$inc=$_SERVER[DOCUMENT_ROOT]."/layout/cache/right.html";
$script="/layout/right.inc" ;
make_cache($inc,$script,10);
?>
</table></td></tr>
<? include "foot.inc"; ?>

페이지를 몇개로 분할한 다음 각부분을 별도로 캐쉬할 수 있습니다.
시간은 분단위입니다.
하지만 스쿨처럼 글을 쓰고 나서도 초기화면에 나타나지않으면 일종의 불안감을 조성하는 경향이 있어서
글이나 댓글을 입력하는 부분에

$inc=$_SERVER[DOCUMENT_ROOT]."/banner/cache/body.html";
$script="/body.inc" ;
new_cache($inc,$script);

를 추가해주면 즉시 반영이 됩니다.


if(file_exists($file))  @include $file; 부분을
if(file_exists($file))  return file_get_contents($file); 했더니 HTML코드를 소스채 뿌려버리는군요.

 

출처 : PHPSCHOOL