Web Cache
當每次client browser都向server請求資源時,若距離遙遠,回應時間慢,那麼體驗品質下降,此時可以藉由 Web Cache (Proxy Server)解決。
Web Cache 是原Server的代理,替代Server對Client提供資料。當Client向Server請求資源時,會先到Web Cache請求。若Web Cache有資料時,先向原Server確定是否有修改,若無修改,Server不會回傳完整資料,告訴Cache沒有修改(如 HTTP/1.1 304 Not Modified),讓他直接回傳先前的給客戶,否則Server回傳完整資料。若Cache沒有資料時,會對原Server請求完整資料再回傳給Client,並將資料存在Cache。透過分散各地的Web Cache,因為靠近Client,整體的網頁反應時間下降。
Browser Cache
瀏覽器快取是私有快取,存在於使用者的Browser之中,透過一些判斷機制免去向Server請求資料。
Cache Header - Cache-control
Cache-control是HTTP/1.1用來指示快取機制。不論到期時間長短,優先性高於Expires header。
Cache-Control: no-store
Web Cache 不要存取快取,每次重新請求。
Cache-Control: no-cache
Web Cache 存取快取,但每次重新向原Server驗證是否過期。
Cache-Control: private
快取只給私有使用者使用。不能被公共Web Cache儲存。
Cache-Control: public
快取是公共的。
Cache-Control: max-age=< seconds >
快取有多少時間不需要被更新。對於不常更新的檔案可以使用此機制。
Cache-Control: must-revalidate
Web Cache每次重新向原Server驗證資料是否過期並必須得到回應,才會回應Client。
Cache-Control: max-age=0 與 Cache-Control: no-cache效果類似,雖然也與Cache-Control: must-revalidate相似,但後者保證不會回傳過期資料。
用來驗證Cache的Headers
ETags
強驗證。server 回應 client Etags header,client把其值代入if-None-Match header作為未來請求驗證是否過期。
Last-Modified
弱驗證(因為最小單位是秒)。server 回應 client Last-Modified header,client把其值代入If-Modified-Since header驗證是否過期。
Vary header
Vary header可以指示哪些header更改時,不使用快取資料。