mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 15:00:26 +08:00
支持缓存策略全局的缓存条件/X-Cache中加入更多信息
This commit is contained in:
1
go.mod
1
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
|
||||
)
|
||||
|
||||
@@ -6,6 +6,9 @@ type Reader interface {
|
||||
// Init 初始化
|
||||
Init() error
|
||||
|
||||
// TypeName 类型名称
|
||||
TypeName() string
|
||||
|
||||
// Status 状态码
|
||||
Status() int
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user