About Cache Control
About Cache Control
About Cache Control
About Cache Control
no-cache:這個 response 是不是一定得確認用的是最新版
是: Cache-Control: no-cache
不是: Cache-Control: max-age=3600
no-cache 指的是「要快取」,但每次在使用 cache 前,都要先向伺服器檢查(revalidate)。
如果使用了 no-cache 或 max-age=0 瀏覽器都會快取該資源,並且會在下一次使用時重新驗證,也就是說,
每次都會發送 HTTP 請求,但如果當內容還是有效的話,就不會再下載 HTTP body:
Cache-Control: no-cache, max-age=0
max-age:時間未到不會向伺服器發送請求
// 以下面的例子來說,在接收 response 要超過 10 秒後,瀏覽器才會再次向伺服器發送請求
func ApplyCacheControl(ctx *gin.Context) {
ctx.Header(“Cache-Control”, “max-age=10”)
ctx.Next()
}
cache-control 後使用 max-age,可以讓使用者收到 response 後的一定時間內(max-age),
瀏覽器不會重新發送請求,而是收到(Status code 200 (from memory cache))max-age 後接的是「秒數」
如果同時設置了 max-age 和 Expires 的話,會以 max-age 為主,Expires 將會被忽略。