Files
EdgeCommon/pkg/serverconfigs/http_cache_policy.go

130 lines
3.7 KiB
Go
Raw Normal View History

2020-09-13 19:27:47 +08:00
package serverconfigs
import (
2020-09-26 08:07:24 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2020-09-13 19:27:47 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"strings"
"time"
)
var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"}
// 缓存策略配置
2020-09-16 09:09:31 +08:00
type HTTPCachePolicy struct {
2020-09-20 16:27:59 +08:00
Id int64 `yaml:"id" json:"id"`
2020-09-13 19:27:47 +08:00
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启 TODO
Name string `yaml:"name" json:"name"` // 名称
2020-09-20 11:56:22 +08:00
Key string `yaml:"key" json:"key"` // 每个缓存的Key规则里面可以有变量
2020-09-16 09:09:31 +08:00
Capacity *shared.SizeCapacity `yaml:"capacity" json:"capacity"` // 最大内容容量
Life *shared.TimeDuration `yaml:"life" json:"life"` // 时间
2020-09-20 11:56:22 +08:00
Status []int `yaml:"status" json:"status"` // 缓存的状态码列表
2020-09-16 09:09:31 +08:00
MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 能够请求的最大尺寸
2020-09-13 19:27:47 +08:00
SkipResponseCacheControlValues []string `yaml:"skipCacheControlValues" json:"skipCacheControlValues"` // 可以跳过的响应的Cache-Control值
SkipResponseSetCookie bool `yaml:"skipSetCookie" json:"skipSetCookie"` // 是否跳过响应的Set-Cookie Header
EnableRequestCachePragma bool `yaml:"enableRequestCachePragma" json:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache
2020-09-29 17:23:11 +08:00
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 请求条件
2020-09-13 19:27:47 +08:00
life time.Duration
maxSize int64
capacity int64
uppercaseSkipCacheControlValues []string
Type string `yaml:"type" json:"type"` // 类型
Options map[string]interface{} `yaml:"options" json:"options"` // 选项
}
// 获取新对象
2020-09-16 09:09:31 +08:00
func NewHTTPCachePolicy() *HTTPCachePolicy {
return &HTTPCachePolicy{
2020-09-13 19:27:47 +08:00
SkipResponseCacheControlValues: DefaultSkippedResponseCacheControlValues,
SkipResponseSetCookie: true,
}
}
// 从文件中读取缓存策略
2020-09-16 09:09:31 +08:00
func NewCachePolicyFromFile(file string) *HTTPCachePolicy {
2020-09-13 19:27:47 +08:00
if len(file) == 0 {
return nil
}
reader, err := files.NewReader(Tea.ConfigFile(file))
if err != nil {
logs.Error(err)
return nil
}
defer func() {
_ = reader.Close()
}()
2020-09-16 09:09:31 +08:00
p := NewHTTPCachePolicy()
2020-09-13 19:27:47 +08:00
err = reader.ReadYAML(p)
if err != nil {
logs.Error(err)
return nil
}
return p
}
// 校验
2020-09-20 11:56:22 +08:00
func (this *HTTPCachePolicy) Init() error {
2020-09-13 19:27:47 +08:00
var err error
this.maxSize = this.MaxSize.Bytes()
this.life = this.Life.Duration()
this.capacity = this.Capacity.Bytes()
this.uppercaseSkipCacheControlValues = []string{}
for _, value := range this.SkipResponseCacheControlValues {
this.uppercaseSkipCacheControlValues = append(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
}
// cond
2020-09-29 17:23:11 +08:00
if this.Conds != nil {
err := this.Conds.Init()
if err != nil {
return err
2020-09-13 19:27:47 +08:00
}
}
return err
}
// 最大数据尺寸
2020-09-16 09:09:31 +08:00
func (this *HTTPCachePolicy) MaxDataSize() int64 {
2020-09-13 19:27:47 +08:00
return this.maxSize
}
// 容量
2020-09-16 09:09:31 +08:00
func (this *HTTPCachePolicy) CapacitySize() int64 {
2020-09-13 19:27:47 +08:00
return this.capacity
}
// 生命周期
2020-09-16 09:09:31 +08:00
func (this *HTTPCachePolicy) LifeDuration() time.Duration {
2020-09-13 19:27:47 +08:00
return this.life
}
// 是否包含某个Cache-Control值
2020-09-16 09:09:31 +08:00
func (this *HTTPCachePolicy) ContainsCacheControl(value string) bool {
2020-09-13 19:27:47 +08:00
return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
}
// 检查是否匹配关键词
2020-09-16 09:09:31 +08:00
func (this *HTTPCachePolicy) MatchKeyword(keyword string) (matched bool, name string, tags []string) {
2020-09-13 19:27:47 +08:00
if configutils.MatchKeyword(this.Name, keyword) || configutils.MatchKeyword(this.Type, keyword) {
matched = true
name = this.Name
if len(this.Type) > 0 {
tags = []string{"类型:" + this.Type}
}
}
return
}