20081128 http caching_in_php

36
HTTP Caching in PHP [email protected] Nov. 2008

Upload: hunter-wu

Post on 05-Dec-2014

211 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 20081128 http caching_in_php

HTTP Caching in PHP

[email protected]. 2008

Page 2: 20081128 http caching_in_php

F5 和 Ctrl-F5 的差別?

Page 3: 20081128 http caching_in_php

Table of Contents• HTTP Overview• Rakuya JS Loader

Page 4: 20081128 http caching_in_php

HTTP Overview

Page 5: 20081128 http caching_in_php

A GET Request

GET /index.php HTTP/1.1Host: www.rakuya.com.twUser-Agent:Mozilla/4.0 (...)

HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 355

<html>...

Page 6: 20081128 http caching_in_php

Last-Modified Header

HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 355Last-Modified: Thu, 27 Nov 2008 17:27:48 GMT

<html>...

Page 7: 20081128 http caching_in_php

Last Modified – Request• Conditional Request

由 Server 端再判斷一次是否要更新內容GET /index.php HTTP/1.1Host: www.rakuya.com.twUser-Agent:Mozilla/4.0 (...)If-Modified-Since: Thu, 27 Nov 2008 17:27:48 GMT

HTTP/1.1 304 Not ModifiedContent-Type: text/htmlContent-Length: 0Last-Modified: Thu, 27 Nov 2008 17:27:48 GMT

Page 8: 20081128 http caching_in_php

Last Modified - Ctrl-F5• 清除掉 If-Modified-Since

當成新的 requestGET /index.php HTTP/1.1Host: www.rakuya.com.twUser-Agent:Mozilla/4.0 (...)If-Modified-Since: Thu, 27 Nov 2008 17:27:48 GMT

HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 355

<html>...

Page 9: 20081128 http caching_in_php

Expires Header

HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 355Expires: Wed, 31 Dec 2008 23:59:59 GMT

<html>...

Page 10: 20081128 http caching_in_php

Expires - Before Refresh

• Cached

Page 11: 20081128 http caching_in_php

Expires - Refresh(F5) • New Request

Page 12: 20081128 http caching_in_php

DEMO

Page 13: 20081128 http caching_in_php

正解• F5 和 Ctrl-F5

都是重新向 server 發出 request• F5

解除 Expires 造成的 local 端的 cache• Ctrl-F5 

清除 Last-Modified ( 或 ETag) 造成的 conditional request

Page 14: 20081128 http caching_in_php

但是要小心不是每個瀏覽器的實做方式都一樣

Page 15: 20081128 http caching_in_php

幸好F5 和 Ctrl-F5 在 Firefox 作用相似

Page 16: 20081128 http caching_in_php

不幸的是Google Chrome 實做方式不一樣

F5 和 Ctrl-F5 分不太出來差別 ...

Page 17: 20081128 http caching_in_php

補充一點

Page 18: 20081128 http caching_in_php

ETag Header• 與 Last-Modifed 相似,但不限於使用日期,常見的使用方式是給檔案的 MD5

HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 355Etag: fc5e038d38a57032085441e7fe7010b0

<html>...

Page 19: 20081128 http caching_in_php

ETag Header - Request• Conditional Request

一樣由 Server 端判斷是否要更新內容 GET /index.php HTTP/1.1Host: www.rakuya.com.twUser-Agent:Mozilla/4.0 (...)If-None-Match: fc5e038d38a57032085441e7fe7010b0

HTTP/1.1 304 Not ModifiedContent-Type: text/htmlContent-Length: 0Etag: fc5e038d38a57032085441e7fe7010b0

Page 20: 20081128 http caching_in_php

ETag Header - Ctrl-F5• 清除 If-None-Matched

當成新的 requestGET /index.php HTTP/1.1Host: www.rakuya.com.twUser-Agent:Mozilla/4.0 (...)If-None-Match: fc5e038d38a57032085441e7fe7010b0

HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 355

<html>...

Page 21: 20081128 http caching_in_php

Rakuya JS Loader

Page 22: 20081128 http caching_in_php

Objective• 使用 php 實做• 多檔合併 ( 第一次 Request  速度加快 )• Cache ( 減少 Request)• Conditional Request ( 第二次 Request 時速度加快 )

• 任一檔案被修改時能即時送出正確的結果• pack ( 減少檔案大小 )

Page 23: 20081128 http caching_in_php

Flow• 送出 Expires Header• 判斷並處理 If-Modified-Since

若無更新,則送出 HTTP 304• 讀取並處理所有檔案 (Disk IO)• 判斷並處理 If-None-Match

若無更新,則送出 HTTP 304• 完整 Output

Page 24: 20081128 http caching_in_php

Usage• loader.php?a.js&b.js&c.js&...

透過 ETag 來處理檔案的 md5 ,檔案一更新,就會立即更新 Ex:

http://www.rakuya.com.tw/js/loader.php?json2.pack.js&jquery-1.2.6.pack.js&jquery.blockUI.pack.js&jquery.mousewheel.pack.js&jquery.scrollTo-min.js&ui.core.pack.js&ui.slider.pack.js&item.js&fl.js&ad.js&map_libs_stable/mapstation.js&map_libs_stable/RA_hintwindow.js&map_libs_stable/RA_infowindow_2.js&map_libs_stable/RakuyaMapExt.js&RAKUYA.js&RAKUYA.search.js&RAKUYA.searchHistory.js&jquery.autocomplete.pack.js&RAKUYA.data.cityarea.js&jquery.scrollbox.js&RAKUYA.excel.menu.js

PHP:header('Etag: '. md5($file_contents));

Page 25: 20081128 http caching_in_php

Options• &ver=20081105• &ver=20081105112233

透過 Last-Modifed 產生 Conditional Request ,可減少 Disk IO

PHP:header('Last-Modified: '. gmdate(DATE_RFC1123));

Page 26: 20081128 http caching_in_php

Options - cont.• &opt=packall

全部檔案一起做 pack ( 事實上我也沒用過 )

• &opt=plaintext 不做壓縮,方便除錯

Page 27: 20081128 http caching_in_php

建議使用方式• DEV 開發時

把 js 加到 loader 的白名單內 (pack用 ) 先不使用 &ver= 的參數

• 正式機上 加上 &ver= 的參數,減少 Disk IO

Page 28: 20081128 http caching_in_php

請注意

Page 29: 20081128 http caching_in_php

每個 statement 都要加上 ;var foo = 1;var bar = function() { alert(foo);};

function bar2() { bar();}

Page 30: 20081128 http caching_in_php

TODO• Remove BOM From UTF-8• Minify Option• Server-Side File Caching• URL Rewrite• Gzip Encoding• CSS Loader Enhancement

Page 31: 20081128 http caching_in_php

Frond-End 的成果 ( 首頁 )

Page 32: 20081128 http caching_in_php

Achieved• Rule 1: Make Fewer HTTP Requests• Rule 3: Add an Expires Header• Rule 5: Put Stylesheets at the Top• Rule 6: Put Scripts at the Bottom• Rule 7: Avoid CSS Expressions• Rule 8: Make JavaScript and CSS External

• Rule 10: Minify JavaScript• Rule 13: Configure ETags

Page 33: 20081128 http caching_in_php

Further Study

Page 34: 20081128 http caching_in_php

Recommended Links• Exceptional Performance (YDN)http://developer.yahoo.com/performance/

• Combine - Another JS/CSS Loaderhttp://rakaz.nl/extra/code/combine

Page 35: 20081128 http caching_in_php

Any Questions?

Page 36: 20081128 http caching_in_php

好康分享