2020-09-20 16:27:59 +08:00
|
|
|
|
package serverconfigs
|
|
|
|
|
|
|
2020-10-04 20:38:03 +08:00
|
|
|
|
import (
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
|
|
|
|
"github.com/iwind/TeaGo/lists"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"}
|
|
|
|
|
|
|
2020-09-20 16:27:59 +08:00
|
|
|
|
type HTTPCacheRef struct {
|
2020-10-04 20:38:03 +08:00
|
|
|
|
IsOn bool `yaml:"isOn" json:"isOn"`
|
|
|
|
|
|
CachePolicyId int64 `yaml:"cachePolicyId" json:"cachePolicyId"`
|
|
|
|
|
|
|
|
|
|
|
|
Key string `yaml:"key" json:"key"` // 每个缓存的Key规则,里面可以有变量
|
|
|
|
|
|
Life *shared.TimeDuration `yaml:"life" json:"life"` // 时间
|
|
|
|
|
|
Status []int `yaml:"status" json:"status"` // 缓存的状态码列表
|
|
|
|
|
|
MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 能够请求的最大尺寸
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 请求条件
|
|
|
|
|
|
|
|
|
|
|
|
CachePolicy *HTTPCachePolicy `yaml:"cachePolicy" json:"cachePolicy"`
|
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
|
lifeSeconds int64
|
2020-10-04 20:38:03 +08:00
|
|
|
|
maxSize int64
|
|
|
|
|
|
uppercaseSkipCacheControlValues []string
|
2020-09-20 16:27:59 +08:00
|
|
|
|
}
|
2020-09-26 08:07:24 +08:00
|
|
|
|
|
|
|
|
|
|
func (this *HTTPCacheRef) Init() error {
|
2020-10-04 20:38:03 +08:00
|
|
|
|
if this.MaxSize != nil {
|
|
|
|
|
|
this.maxSize = this.MaxSize.Bytes()
|
|
|
|
|
|
}
|
|
|
|
|
|
if this.Life != nil {
|
2020-10-05 16:54:21 +08:00
|
|
|
|
this.lifeSeconds = int64(this.Life.Duration().Seconds())
|
2020-10-04 20:38:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// control-values
|
|
|
|
|
|
this.uppercaseSkipCacheControlValues = []string{}
|
|
|
|
|
|
for _, value := range this.SkipResponseCacheControlValues {
|
|
|
|
|
|
this.uppercaseSkipCacheControlValues = append(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// conds
|
|
|
|
|
|
if this.Conds != nil {
|
|
|
|
|
|
err := this.Conds.Init()
|
2020-10-02 17:22:46 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-04 20:38:03 +08:00
|
|
|
|
|
|
|
|
|
|
// policy
|
|
|
|
|
|
if this.CachePolicy != nil {
|
|
|
|
|
|
err := this.CachePolicy.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-26 08:07:24 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2020-10-04 20:38:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 最大数据尺寸
|
2020-10-05 16:54:21 +08:00
|
|
|
|
func (this *HTTPCacheRef) MaxSizeBytes() int64 {
|
2020-10-04 20:38:03 +08:00
|
|
|
|
return this.maxSize
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 生命周期
|
2020-10-05 16:54:21 +08:00
|
|
|
|
func (this *HTTPCacheRef) LifeSeconds() int64 {
|
|
|
|
|
|
return this.lifeSeconds
|
2020-10-04 20:38:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 是否包含某个Cache-Control值
|
|
|
|
|
|
func (this *HTTPCacheRef) ContainsCacheControl(value string) bool {
|
|
|
|
|
|
return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
|
|
|
|
|
|
}
|