func NewCache(res *http.Response) *URLCache { directive := res.Header.Get("Cache-Control") cc := cachecontrol.Parse(directive) noCache, _ := cc.NoCache() if len(directive) == 0 || noCache || cc.NoStore() { return nil } now := time.Now() lm := res.Header.Get("Last-Modified") etag := res.Header.Get("ETag") body, err := ioutil.ReadAll(res.Body) if err != nil { return nil } res.Body.Close() md5 := GetMD5(body) if len(body) >= 1024*1024*4 { body = []byte{} } res.Body = &ClosableBuffer{bytes.NewReader(body)} res.Header.Set(CachedHeader, CachedHeaderVal) res.Header.Set(CachedMD5Header, md5) return &URLCache{ LastModified: lm, Etag: etag, ExpiresAt: now.Add(cc.MaxAge()), CacheControl: &cc, CachedResponse: res, CachedBody: body, MD5: md5, } }
func NewURLCache(res *http.Response) (*URLCache, string) { directive := res.Header.Get("Cache-Control") cc := cachecontrol.Parse(directive) noCache, _ := cc.NoCache() md5 := util.GetMD5ByIO(res.Body) if len(directive) == 0 || noCache || cc.NoStore() { return nil, md5 } now := time.Now() lm := res.Header.Get("Last-Modified") etag := res.Header.Get("ETag") return &URLCache{ LastModified: lm, Etag: etag, ExpiresAt: now.Add(cc.MaxAge()), CacheControl: &cc, MD5: md5, }, md5 }