Cache-Control: max-ageの実験

6454 ワード

サーバー

go言語で実験用のサーバーを実装

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
)

func handler(w http.ResponseWriter, r *http.Request) {
	// Cache-Control実験
	w.Header().Add("Cache-Control", "max-age=180")
	dump, err := httputil.DumpRequest(r, true)
	if err != nil {
		http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
		return
	}
	fmt.Println(string(dump))
	fmt.Fprintf(w, "<html><body>hello</body></html>")
}

func main() {
	var httpServer http.Server
	http.HandleFunc("/", handler)
	log.Println("start http listening : 18888")
	httpServer.Addr = ":18888"
	log.Println(httpServer.ListenAndServe())
}

ブラウザでアクセス

ブラウザはChrom(バージョン: 100.0.4896.88)を使用

初回アクセス時のヘッダー

General
	Request URL: http://localhost:18888/
	Request Method: GET
	Status Code: 200 OK
	Remote Address: [::1]:18888
	Referrer Policy: strict-origin-when-cross-origin
Response Header
	Cache-Control: max-age=180
	Content-Length: 31
	Content-Type: text/html; charset=utf-8
	Date: Thu, 21 Apr 2022 13:28:47 GMT
Request Header
	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
	Accept-Encoding: gzip, deflate, br
	Accept-Language: ja,en-US;q=0.9,en;q=0.8
	Connection: keep-alive
	Cookie: BetterErrors-has-used-console=true
	Host: localhost:18888
	sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"
	sec-ch-ua-mobile: ?0
	sec-ch-ua-platform: "macOS"
	Sec-Fetch-Dest: document
	Sec-Fetch-Mode: navigate
	Sec-Fetch-Site: none
	Sec-Fetch-User: ?1
	Upgrade-Insecure-Requests: 1
	User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

2回目アクセス時のヘッダー

General
	Request URL: http://localhost:18888/
	Request Method: GET
	Status Code: 200 OK (from disk cache)
	Remote Address: [::1]:18888
	Referrer Policy: strict-origin-when-cross-origin
Response Header
	Cache-Control: max-age=180
	Content-Length: 31
	Content-Type: text/html; charset=utf-8
	Date: Thu, 21 Apr 2022 13:28:47 GMT
  • 2回めのアクセスではリクエストヘッダーは存在しない
  • Status Code: 200 OK (from disk cache) となっておりキャッシュを使用していることがわかる