動態網頁也可以被快取喔!

話說 GIGAWebAMP 服務推出有一陣子了

很多人都以為 WebAMP 只能給靜態網頁使用,動態網頁無法得到任何加速效果

但其實只要程式設計得宜,動態網頁也有可能可以享受到 WebAMP 的網站超頻加速的效果喔

以下我們用一個簡單的例子來說明:

首先,我們先看這個原始圖片,大小約為 1.4 MBytes:

http://bbs.giga.net.tw/demo/Peacock.jpg

由於這個檔案是靜態檔案,因此理所當然會被 WebAMP 加速 (HIT),我們用 wget 來驗證看看:

# wget -v -S 'http://bbs.giga.net.tw/demo/Peacock.jpg'
--10:07:00--  http://bbs.giga.net.tw/demo/Peacock.jpg
           => `Peacock.jpg'
Resolving bbs.giga.net.tw... done.
Connecting to bbs.giga.net.tw[203.187.29.180]:80... connected.
HTTP request sent, awaiting response...
 1 HTTP/1.0 200 OK
 2 Date: Sat, 12 Mar 2005 15:25:22 GMT
 3 Server: Apache/2.0.52 (FreeBSD) PHP/5.0.3
 4 Last-Modified: Tue, 18 May 2004 08:53:23 GMT
 5 ETag: "51b65-1680be-12ff86c0"
 6 Accept-Ranges: bytes
 7 Content-Length: 1474750
 8 Content-Type: image/jpeg
 9 Age: 259658
10 X-Cache: HIT from WebAmpRP@GIGAMEDIA
11 Connection: close

100%[====================================>] 1,474,750    974.42K/s

接下來,我們測試一下用程式來動態輸出這個圖片,首先我們寫了一支 PHP 程式:

<?
#   http://bbs.giga.net.tw/demo/dl-test1.php

    $filename = 'Peacock.jpg';
 
    header( 'Content-Type: application/octet-stream' );
    header( 'Content-Length: ' . filesize($filename) );
    header( 'Content-Transfer-Encoding: binary' );
    header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
 
    $fp = fopen( $filename, 'rb' );
    while( !feof($fp) ) {
            echo fread( $fp, 1024 );
    }
    fclose($fp);
?>

然後,我們用 wget 測試幾次看看:

# wget -v -S 'http://bbs.giga.net.tw/demo/dl-test1.php'
--10:14:29--  http://bbs.giga.net.tw/demo/dl-test1.php
           => `dl-test1.php'
Resolving bbs.giga.net.tw... done.
Connecting to bbs.giga.net.tw[203.187.29.180]:80... connected.
HTTP request sent, awaiting response...
 1 HTTP/1.0 200 OK
 2 Date: Mon, 01 Aug 2005 02:14:33 GMT
 3 Server: Apache/2.0.54 (FreeBSD) PHP/5.0.4
 4 X-Powered-By: PHP/5.0.4
 5 Content-Length: 1474750
 6 Content-Transfer-Encoding: binary
 7 Content-Disposition: attachment; filename="Peacock.jpg"
 8 Content-Type: application/octet-stream
 9 X-Cache: MISS from WebAmpRP@GIGAMEDIA
10 Connection: close

100%[====================================>] 1,474,750      325.21K/s

我們會發覺不管測試幾次,都會是 MISS 的情形!

接下來,我們稍微修改一下程式,加上 Last-Modified: 的 HTTP header …

<?
#   http://bbs.giga.net.tw/demo/dl-test2.php

    $filename = 'Peacock.jpg';
 
    header( 'Last-Modified: ' .
            gmdate('D, d M Y H:i:s', filemtime($filename) ) . ' GMT');
    header( 'Content-Type: application/octet-stream' );
    header( 'Content-Length: ' . filesize($filename) );
    header( 'Content-Transfer-Encoding: binary' );
    header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
 
    $fp = fopen( $filename, 'rb' );
    while( !feof($fp) ) {
            echo fread( $fp, 1024 );
    }
    fclose($fp);
?>

上面 filemtime($filename) 主要是拿檔案的修改時間來當作 Last-Modified: 時間,不過要用 gmdate() 把 UNIX Timestamp 轉換成 GMT 格林威治時區的時間

加了上面 Last-Modified 這一行之後,我們再來測試一下:

% wget -v -S 'http://bbs.giga.net.tw/demo/dl-test2.php'
--09:56:23--  http://bbs.giga.net.tw/demo/dl-test2.php
           => `dl-test2.php'
Resolving bbs.giga.net.tw... done.
Connecting to bbs.giga.net.tw[203.187.29.180]:80... connected.
HTTP request sent, awaiting response...
 1 HTTP/1.0 200 OK
 2 Date: Mon, 01 Aug 2005 01:56:25 GMT
 3 Server: Apache/2.0.54 (FreeBSD) PHP/5.0.4
 4 X-Powered-By: PHP/5.0.4
 5 Last-Modified: Tue, 18 May 2004 08:53:23 GMT
 6 Content-Length: 1474750
 7 Content-Transfer-Encoding: binary
 8 Content-Disposition: attachment; filename="Peacock.jpg"
 9 Content-Type: application/octet-stream
10 X-Cache: MISS from WebAmpRP@GIGAMEDIA
11 Connection: close

100%[====================================>] 1,474,750    350.20K/s

第一次當然還是 MISS,不過第二次以後就都是 HIT

# wget -v -S 'http://bbs.giga.net.tw/demo/dl-test2.php'
--09:56:26--  http://bbs.giga.net.tw/demo/dl-test2.php
           => `dl-test2.php'
Resolving bbs.giga.net.tw... done.
Connecting to bbs.giga.net.tw[203.187.29.180]:80... connected.
HTTP request sent, awaiting response...
 1 HTTP/1.0 200 OK
 2 Date: Mon, 01 Aug 2005 01:56:25 GMT
 3 Server: Apache/2.0.54 (FreeBSD) PHP/5.0.4
 4 X-Powered-By: PHP/5.0.4
 5 Last-Modified: Tue, 18 May 2004 08:53:23 GMT
 6 Content-Length: 1474750
 7 Content-Transfer-Encoding: binary
 8 Content-Disposition: attachment; filename="Peacock.jpg"
 9 Content-Type: application/octet-stream
10 Age: 4
11 X-Cache: HIT from WebAmpRP@GIGAMEDIA
12 Connection: close

100%[====================================>] 1,474,750    950.12K/s

以上是以圖片作例子,你也可以代換成要被下載的檔案 (有些人會用這種方式來計算檔案被下載的次數)

甚至,討論區的文章如果能被正確貼上 Last-Modified: 的 header,也有可能可以享受到 WebAMP 加速的好處喔

更進一步的資訊,請參考下一篇「動態網頁也可以被快取喔! (Part II)

在〈動態網頁也可以被快取喔!〉中有 1 則留言

歡迎留下您的意見