跳至主要内容

Http Cache

Targets of caching operations

HTTP caching is optional, but reusing a cached resource is usually desirable. However, common HTTP caches are typically limited to caching responses to GET and may decline other methods. The primary cache key consists of the request method and target URI (oftentimes only the URI is used as only GET requests are caching targets). Common forms of caching entries are:

Successful results of a retrieval request: a 200 (OK) response to a GET request containing a resource like HTML documents, images or files. Permanent redirects: a 301 (Moved Permanently) response. Error responses: a 404 (Not Found) result page. Incomplete results: a 206 (Partial Content) response. Responses other than GET if something suitable for use as a cache key is defined. A cache entry might also consist of multiple stored responses differentiated by a secondary key, if the request is target of content negotiation. For more details see the information about the Vary header below.

Pros
  • 增快網頁載入速度
  • 減少 Server 負擔
Cons
  • 有機會拿到 Outdated Data
Respond HeaderPragmaCache-ControlExpiresE TagLast-Modified
快取過期相對時間 ( 單位:秒)絕對時間Key絕對時間
優先級最高中高中低最低
快取類型強快取強快取強快取協商快取協商快取
排他性XXXX

Pragma

備註

no-cache

Cache-Control

備註
  • public: 所有網路設備都可以 cache
  • private: 只有瀏覽器可以 cache
  • no-cache: 瀏覽器可以 cache,但是要先驗證
  • no-store: 不可以 cache

Expires

備註
  • Client 跟 Server 時間不一定一致,有可能有 exception 假使使用者將 client time 改到未來,那就永遠都會重新拿

E Tag

備註

在接收到第一次 server response 後,後續 request header 都會帶上 If-None-Match 也就是上次回覆的 E Tag

Last-Modified

備註

在接收到第一次 server response 後,後續 request header 都會帶上 If-Modified-Since 也就是上次回覆的 Last-Modified