From 00fcd53ffb74f3e2eefaedf6475621eb7173bfe0 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 2 Dec 2021 10:19:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=94=AF=E6=8C=81=E6=BA=90?= =?UTF-8?q?=E7=AB=99=E8=AE=BE=E7=BD=AE=E7=9A=84max-age?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_writer.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index fb40120..cd30a7a 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -638,9 +638,26 @@ func (this *HTTPWriter) prepareCache(size int64) { this.cacheStorage = storage life := cacheRef.LifeSeconds() - if life <= 60 { // 最小不能少于1分钟 + + if life <= 0 { life = 60 } + + // 支持源站设置的max-age + if this.req.web.Cache != nil && this.req.web.Cache.EnableCacheControlMaxAge { + var cacheControl = this.Header().Get("Cache-Control") + var pieces = strings.Split(cacheControl, ";") + for _, piece := range pieces { + var eqIndex = strings.Index(piece, "=") + if eqIndex > 0 && piece[:eqIndex] == "max-age" { + var maxAge = types.Int64(piece[eqIndex+1:]) + if maxAge > 0 { + life = maxAge + } + } + } + } + expiredAt := utils.UnixTime() + life var cacheKey = this.req.cacheKey if this.webpIsEncoding {