From 46ecb8e47d693a120c61ada2012846be6cd9db06 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Mon, 24 May 2021 09:23:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=BC=93=E5=AD=98=E7=AD=96?= =?UTF-8?q?=E7=95=A5=E5=85=A8=E5=B1=80=E7=9A=84=E7=BC=93=E5=AD=98=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6/X-Cache=E4=B8=AD=E5=8A=A0=E5=85=A5=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + internal/caches/reader.go | 3 +++ internal/caches/reader_file.go | 4 ++++ internal/caches/reader_memory.go | 4 ++++ internal/caches/storage_file.go | 21 +++++----------- internal/nodes/http_request_cache.go | 36 +++++++++++++++++++++------- 6 files changed, 45 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index f67c743..ad94d82 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/shirou/gopsutil v2.20.9+incompatible golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 + golang.org/x/text v0.3.2 google.golang.org/grpc v1.32.0 gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) diff --git a/internal/caches/reader.go b/internal/caches/reader.go index aa9c7f0..bee974d 100644 --- a/internal/caches/reader.go +++ b/internal/caches/reader.go @@ -6,6 +6,9 @@ type Reader interface { // Init 初始化 Init() error + // TypeName 类型名称 + TypeName() string + // Status 状态码 Status() int diff --git a/internal/caches/reader_file.go b/internal/caches/reader_file.go index 1fdb50c..6c15edc 100644 --- a/internal/caches/reader_file.go +++ b/internal/caches/reader_file.go @@ -106,6 +106,10 @@ func (this *FileReader) Init() error { return nil } +func (this *FileReader) TypeName() string { + return "disk" +} + func (this *FileReader) Status() int { return this.status } diff --git a/internal/caches/reader_memory.go b/internal/caches/reader_memory.go index c57e59d..27b416c 100644 --- a/internal/caches/reader_memory.go +++ b/internal/caches/reader_memory.go @@ -16,6 +16,10 @@ func (this *MemoryReader) Init() error { return nil } +func (this *MemoryReader) TypeName() string { + return "memory" +} + func (this *MemoryReader) Status() int { return this.item.Status } diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index ba5b1df..0c6af3e 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -12,6 +12,8 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/Tea" stringutil "github.com/iwind/TeaGo/utils/string" + "golang.org/x/text/language" + "golang.org/x/text/message" "io" "os" "path/filepath" @@ -130,19 +132,8 @@ func (this *FileStorage) Init() error { defer func() { // 统计 - count := 0 - size := int64(0) - if this.list != nil { - stat, err := this.list.Stat(func(hash string) bool { - return true - }) - if err != nil { - remotelogs.Error("CACHE", "stat cache "+strconv.FormatInt(this.policy.Id, 10)+" failed: "+err.Error()) - } else { - count = stat.Count - size = stat.Size - } - } + count := stat.Count + size := stat.Size cost := time.Since(before).Seconds() * 1000 sizeMB := strconv.FormatInt(size, 10) + " Bytes" @@ -153,7 +144,7 @@ func (this *FileStorage) Init() error { } else if size > 1024 { sizeMB = fmt.Sprintf("%.3f K", float64(size)/1024) } - remotelogs.Println("CACHE", "init policy "+strconv.FormatInt(this.policy.Id, 10)+" from '"+cacheDir+"', cost: "+fmt.Sprintf("%.2f", cost)+" ms, count: "+strconv.Itoa(count)+", size: "+sizeMB) + remotelogs.Println("CACHE", "init policy "+strconv.FormatInt(this.policy.Id, 10)+" from '"+cacheDir+"', cost: "+fmt.Sprintf("%.2f", cost)+" ms, count: "+message.NewPrinter(language.English).Sprintf("%d", count)+", size: "+sizeMB) }() // 初始化list @@ -530,7 +521,7 @@ func (this *FileStorage) Stop() { this.memoryStorage.Stop() } - this.list.Reset() + _ = this.list.Reset() if this.ticker != nil { this.ticker.Stop() } diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index 32af4c9..99c2d73 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -12,7 +12,12 @@ import ( // 读取缓存 func (this *HTTPRequest) doCacheRead() (shouldStop bool) { - if this.web.Cache == nil || !this.web.Cache.IsOn || len(this.web.Cache.CacheRefs) == 0 { + cachePolicy := sharedNodeConfig.HTTPCachePolicy + if cachePolicy == nil || !cachePolicy.IsOn { + return + } + + if this.web.Cache == nil || !this.web.Cache.IsOn || (len(cachePolicy.CacheRefs) == 0 && len(this.web.Cache.CacheRefs) == 0) { return } var addStatusHeader = this.web.Cache.AddStatusHeader @@ -25,12 +30,8 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { }() } - cachePolicy := sharedNodeConfig.HTTPCachePolicy - if cachePolicy == nil || !cachePolicy.IsOn { - return - } - - // 检查条件 + // 检查服务独立的缓存条件 + refType := "" for _, cacheRef := range this.web.Cache.CacheRefs { if !cacheRef.IsOn || cacheRef.Conds == nil || @@ -39,11 +40,28 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { } if cacheRef.Conds.MatchRequest(this.Format) { this.cacheRef = cacheRef + refType = "server" break } } if this.cacheRef == nil { - return + // 检查策略默认的缓存条件 + for _, cacheRef := range cachePolicy.CacheRefs { + if !cacheRef.IsOn || + cacheRef.Conds == nil || + !cacheRef.Conds.HasRequestConds() { + continue + } + if cacheRef.Conds.MatchRequest(this.Format) { + this.cacheRef = cacheRef + refType = "policy" + break + } + } + + if this.cacheRef == nil { + return + } } // 相关变量 @@ -130,7 +148,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { } if addStatusHeader { - this.writer.Header().Set("X-Cache", "HIT") + this.writer.Header().Set("X-Cache", "HIT, "+refType+", "+reader.TypeName()) } this.processResponseHeaders(reader.Status())