From ebf4d41290b01c50695e22a66b4c2e72ee8e8119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 12 May 2021 16:10:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0X-Cache=20Header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_cache.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index f175f93..046a5f1 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -15,6 +15,15 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { if this.web.Cache == nil || !this.web.Cache.IsOn || len(this.web.Cache.CacheRefs) == 0 { return } + var addStatusHeader = this.web.Cache.AddStatusHeader + if addStatusHeader { + defer func() { + cacheStatus := this.varMapping["cache.status"] + if cacheStatus != "HIT" { + this.writer.Header().Set("X-Cache", cacheStatus) + } + }() + } cachePolicy := sharedNodeConfig.HTTPCachePolicy if cachePolicy == nil || !cachePolicy.IsOn { @@ -115,6 +124,9 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { return } + if addStatusHeader { + this.writer.Header().Set("X-Cache", "HIT") + } this.processResponseHeaders(reader.Status()) // 输出Body