支持缓存策略全局的缓存条件/X-Cache中加入更多信息

This commit is contained in:
GoEdgeLab
2021-05-24 09:23:51 +08:00
parent 342568d71f
commit 46ecb8e47d
6 changed files with 45 additions and 24 deletions

1
go.mod
View File

@@ -20,6 +20,7 @@ require (
github.com/shirou/gopsutil v2.20.9+incompatible github.com/shirou/gopsutil v2.20.9+incompatible
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 golang.org/x/sys v0.0.0-20200519105757-fe76b779f299
golang.org/x/text v0.3.2
google.golang.org/grpc v1.32.0 google.golang.org/grpc v1.32.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
) )

View File

@@ -6,6 +6,9 @@ type Reader interface {
// Init 初始化 // Init 初始化
Init() error Init() error
// TypeName 类型名称
TypeName() string
// Status 状态码 // Status 状态码
Status() int Status() int

View File

@@ -106,6 +106,10 @@ func (this *FileReader) Init() error {
return nil return nil
} }
func (this *FileReader) TypeName() string {
return "disk"
}
func (this *FileReader) Status() int { func (this *FileReader) Status() int {
return this.status return this.status
} }

View File

@@ -16,6 +16,10 @@ func (this *MemoryReader) Init() error {
return nil return nil
} }
func (this *MemoryReader) TypeName() string {
return "memory"
}
func (this *MemoryReader) Status() int { func (this *MemoryReader) Status() int {
return this.item.Status return this.item.Status
} }

View File

@@ -12,6 +12,8 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
stringutil "github.com/iwind/TeaGo/utils/string" stringutil "github.com/iwind/TeaGo/utils/string"
"golang.org/x/text/language"
"golang.org/x/text/message"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@@ -130,19 +132,8 @@ func (this *FileStorage) Init() error {
defer func() { defer func() {
// 统计 // 统计
count := 0 count := stat.Count
size := int64(0) size := stat.Size
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
}
}
cost := time.Since(before).Seconds() * 1000 cost := time.Since(before).Seconds() * 1000
sizeMB := strconv.FormatInt(size, 10) + " Bytes" sizeMB := strconv.FormatInt(size, 10) + " Bytes"
@@ -153,7 +144,7 @@ func (this *FileStorage) Init() error {
} else if size > 1024 { } else if size > 1024 {
sizeMB = fmt.Sprintf("%.3f K", float64(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 // 初始化list
@@ -530,7 +521,7 @@ func (this *FileStorage) Stop() {
this.memoryStorage.Stop() this.memoryStorage.Stop()
} }
this.list.Reset() _ = this.list.Reset()
if this.ticker != nil { if this.ticker != nil {
this.ticker.Stop() this.ticker.Stop()
} }

View File

@@ -12,7 +12,12 @@ import (
// 读取缓存 // 读取缓存
func (this *HTTPRequest) doCacheRead() (shouldStop bool) { 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 return
} }
var addStatusHeader = this.web.Cache.AddStatusHeader var addStatusHeader = this.web.Cache.AddStatusHeader
@@ -25,12 +30,8 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
}() }()
} }
cachePolicy := sharedNodeConfig.HTTPCachePolicy // 检查服务独立的缓存条件
if cachePolicy == nil || !cachePolicy.IsOn { refType := ""
return
}
// 检查条件
for _, cacheRef := range this.web.Cache.CacheRefs { for _, cacheRef := range this.web.Cache.CacheRefs {
if !cacheRef.IsOn || if !cacheRef.IsOn ||
cacheRef.Conds == nil || cacheRef.Conds == nil ||
@@ -39,11 +40,28 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
} }
if cacheRef.Conds.MatchRequest(this.Format) { if cacheRef.Conds.MatchRequest(this.Format) {
this.cacheRef = cacheRef this.cacheRef = cacheRef
refType = "server"
break break
} }
} }
if this.cacheRef == nil { 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 { if addStatusHeader {
this.writer.Header().Set("X-Cache", "HIT") this.writer.Header().Set("X-Cache", "HIT, "+refType+", "+reader.TypeName())
} }
this.processResponseHeaders(reader.Status()) this.processResponseHeaders(reader.Status())