Flash, Image, JS, CSS 캐싱하기.. Posted On 2008/07/07 01:10, Filed Under WEB/PHP

텍스트 큐브를 설치하고, 스킨을 설치했더니..
스킨에 타이틀 부분 플래시가..
500KB 가 넘는다 ;

회사에서 이미지 캐싱하는 것처럼 캐싱하려고
PHPSchool 을 뒤져서 적용했떠니..
mod_expires 모듈을 지원하지 않는댄다;

공용 서버라서 설치할 수 없다나 머라나..

그래도 다행이 rewrite 모듈은 올라와 있으니..
php로 swf, gif, jpg, png, js, css 파일은 캐싱을 해야지.

RewriteRule 에서 해당 파일을 캐싱하기 위한 php파일로 보내고,
스쿨에서 긁어온 소스를 이용해서,
파일 캐싱..

우선, www 안에 있는 .htaccess 파일에서 RewriteRule 수정

# 각 확장자별로 타입 지정 후 cache.php 파일로 연결 RewriteRule ^(.*\.js)$  /cache.php?type=js&url=\./$1 [L] RewriteRule ^(.*\.css)$ /cache.php?type=css&url=\./$1 [L] RewriteRule ^(.*\.gif)$ /cache.php?type=gif&url=\./$1 [L] RewriteRule ^(.*\.jpg)$ /cache.php?type=jpg&url=\./$1 [L] RewriteRule ^(.*\.png)$ /cache.php?type=png&url=\./$1 [L] RewriteRule ^(.*\.swf)$ /cache.php?type=swf&url=\./$1 [L]


/cache.php 라고 했으니, www 밑에 cache.php 파일을 생성
<?php     $GETFile = $_GET['url'];     $GETType = $_GET['type'];         $aETag->mtime = filemtime( $GETFile );     $aETag->inode = fileinode( $GETFile );     $aETag->size  = filesize(  $GETFile );         $sApacheHeader = apache_request_headers();         if ( isset( $sApacheHeader['If-Modified-Since'] )) {         $sTime = strtotime(preg_replace('/;.*$/', '', $sApacheHeader['If-Modified-Since']));                 if ($aETag->mtime == $sTime) {             header('HTTP/1.1 304');                         $aETag->end = true;         }     }            // 파일 타입별로 컨텐츠 헤더 작성     if ($GETType == 'gif') {         header('Content-Type: image/gif');     } else if ($GETType == 'jpg') {         header('Content-Type: image/jpeg');     } else if ($GETType == 'png') {         header('Content-Type: image/png');     } else if ($GETType == 'swf') {         header( 'Content-Type: application/x-shockwave-flash' );     } else if ($GETType == 'css') {         header('Content-Type: text/css');     } else if ($GETType == 'js') {         header('Content-Type: application/x-javascript');     }         // 캐싱하기 위한 헤더 작성     header( 'Date: '.substr( gmdate( 'r' ), 0, -5).'GMT' );     header( 'Expires: '.substr( gmdate( 'r', strtotime( '+1 MONTH' )), 0, -5 ).'GMT' );     header( 'Cache-Control: private, max-age=2592000' );     header( 'Pragma: cache' );     header( 'Last-Modified: '.substr( gmdate( 'r', $aETag->mtime ), 0, -5 ).'GMT' );       // ETag 추가     $aETag->mtime = dechex( $aETag->mtime );     $aETag->inode = dechex( $aETag->inode );     $aETag->size  = dechex( $aETag->size );       header( 'ETag: '.$aETag->inode .'-'.$aETag->size .'-'.$aETag->mtime );         if ( isset( $aETag->end )) {         exit;     }         echo file_get_contents( $GETFile ); ?>


간단히 테스트한 결과로는..

사용자 삽입 이미지

잘 되는듯?

대충 적용은 해두자.

문제는..
이게 필요한 만큼의 트래픽이 생기느냔 거지만..

2008/07/07 01:10 2008/07/07 01:10

Total: 136526 (Today: 29, Yesterday: 84)

RSS