mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-04-27 08:55:18 +08:00
实现缓存策略部分管理功能
This commit is contained in:
67
pkg/serverconfigs/http_cache_cond.go
Normal file
67
pkg/serverconfigs/http_cache_cond.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"}
|
||||
|
||||
type HTTPCacheCond struct {
|
||||
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"` // 请求条件
|
||||
|
||||
life time.Duration
|
||||
maxSize int64
|
||||
uppercaseSkipCacheControlValues []string
|
||||
}
|
||||
|
||||
func (this *HTTPCacheCond) Init() error {
|
||||
if this.MaxSize != nil {
|
||||
this.maxSize = this.MaxSize.Bytes()
|
||||
}
|
||||
if this.Life != nil {
|
||||
this.life = this.Life.Duration()
|
||||
}
|
||||
|
||||
// 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()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 最大数据尺寸
|
||||
func (this *HTTPCacheCond) MaxDataSize() int64 {
|
||||
return this.maxSize
|
||||
}
|
||||
|
||||
// 生命周期
|
||||
func (this *HTTPCacheCond) LifeDuration() time.Duration {
|
||||
return this.life
|
||||
}
|
||||
|
||||
// 是否包含某个Cache-Control值
|
||||
func (this *HTTPCacheCond) ContainsCacheControl(value string) bool {
|
||||
return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
|
||||
}
|
||||
11
pkg/serverconfigs/http_cache_config_file.go
Normal file
11
pkg/serverconfigs/http_cache_config_file.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package serverconfigs
|
||||
|
||||
type CachePolicyType = string
|
||||
|
||||
type HTTPFileCacheConfig struct {
|
||||
Dir string `yaml:"dir" json:"dir"` // 目录
|
||||
}
|
||||
|
||||
func (this *HTTPFileCacheConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
8
pkg/serverconfigs/http_cache_config_memory.go
Normal file
8
pkg/serverconfigs/http_cache_config_memory.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPMemoryCacheConfig struct {
|
||||
}
|
||||
|
||||
func (this *HTTPMemoryCacheConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,129 +1,36 @@
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"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"}
|
||||
|
||||
// 缓存策略配置
|
||||
type HTTPCachePolicy struct {
|
||||
Id int64 `yaml:"id" json:"id"`
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启 TODO
|
||||
Name string `yaml:"name" json:"name"` // 名称
|
||||
Id int64 `yaml:"id" json:"id"`
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
|
||||
Name string `yaml:"name" json:"name"` // 名称
|
||||
Description string `yaml:"description" json:"description"` // 描述
|
||||
Capacity *shared.SizeCapacity `yaml:"capacity" json:"capacity"` // 最大内容容量
|
||||
MaxKeys int64 `yaml:"maxKeys" json:"maxKeys"` // 最多Key值
|
||||
MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 单个缓存最大尺寸
|
||||
Type CachePolicyType `yaml:"type" json:"type"` // 类型
|
||||
Options map[string]interface{} `yaml:"options" json:"options"` // 选项
|
||||
|
||||
Key string `yaml:"key" json:"key"` // 每个缓存的Key规则,里面可以有变量
|
||||
Capacity *shared.SizeCapacity `yaml:"capacity" json:"capacity"` // 最大内容容量
|
||||
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"` // 请求条件
|
||||
|
||||
life time.Duration
|
||||
maxSize int64
|
||||
capacity int64
|
||||
|
||||
uppercaseSkipCacheControlValues []string
|
||||
|
||||
Type string `yaml:"type" json:"type"` // 类型
|
||||
Options map[string]interface{} `yaml:"options" json:"options"` // 选项
|
||||
}
|
||||
|
||||
// 获取新对象
|
||||
func NewHTTPCachePolicy() *HTTPCachePolicy {
|
||||
return &HTTPCachePolicy{
|
||||
SkipResponseCacheControlValues: DefaultSkippedResponseCacheControlValues,
|
||||
SkipResponseSetCookie: true,
|
||||
}
|
||||
}
|
||||
|
||||
// 从文件中读取缓存策略
|
||||
func NewCachePolicyFromFile(file string) *HTTPCachePolicy {
|
||||
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()
|
||||
}()
|
||||
|
||||
p := NewHTTPCachePolicy()
|
||||
err = reader.ReadYAML(p)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// 校验
|
||||
func (this *HTTPCachePolicy) Init() error {
|
||||
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
|
||||
if this.Conds != nil {
|
||||
err := this.Conds.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if this.Capacity != nil {
|
||||
this.capacity = this.Capacity.Bytes()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 最大数据尺寸
|
||||
func (this *HTTPCachePolicy) MaxDataSize() int64 {
|
||||
return this.maxSize
|
||||
}
|
||||
|
||||
// 容量
|
||||
func (this *HTTPCachePolicy) CapacitySize() int64 {
|
||||
return this.capacity
|
||||
}
|
||||
|
||||
// 生命周期
|
||||
func (this *HTTPCachePolicy) LifeDuration() time.Duration {
|
||||
return this.life
|
||||
}
|
||||
|
||||
// 是否包含某个Cache-Control值
|
||||
func (this *HTTPCachePolicy) ContainsCacheControl(value string) bool {
|
||||
return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
|
||||
}
|
||||
|
||||
// 检查是否匹配关键词
|
||||
func (this *HTTPCachePolicy) MatchKeyword(keyword string) (matched bool, name string, tags []string) {
|
||||
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
|
||||
}
|
||||
|
||||
29
pkg/serverconfigs/http_cache_policy_types.go
Normal file
29
pkg/serverconfigs/http_cache_policy_types.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
const (
|
||||
CachePolicyTypeFile CachePolicyType = "file"
|
||||
CachePolicyTypeMemory CachePolicyType = "memory"
|
||||
)
|
||||
|
||||
var AllCachePolicyTypes = []maps.Map{
|
||||
{
|
||||
"name": "文件缓存",
|
||||
"type": CachePolicyTypeFile,
|
||||
},
|
||||
{
|
||||
"name": "内存缓存",
|
||||
"type": CachePolicyTypeMemory,
|
||||
},
|
||||
}
|
||||
|
||||
// 根据类型查找名称
|
||||
func FindCachePolicyTypeName(policyType CachePolicyType) string {
|
||||
for _, t := range AllCachePolicyTypes {
|
||||
if t.GetString("type") == policyType {
|
||||
return t.GetString("name")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPCacheRef struct {
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否覆盖
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
|
||||
CachePolicyId int64 `yaml:"cachePolicyId" json:"cachePolicyId"` // 缓存策略ID
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否覆盖
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
|
||||
CachePolicyId int64 `yaml:"cachePolicyId" json:"cachePolicyId"` // 缓存策略ID
|
||||
Cond *HTTPCacheCond `yaml:"cond" json:"cond"` // 条件
|
||||
}
|
||||
|
||||
func (this *HTTPCacheRef) Init() error {
|
||||
if this.Cond != nil {
|
||||
err := this.Cond.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ type HTTPWebConfig struct {
|
||||
MaxRequestBodySize string `yaml:"maxRequestBodySize" json:"maxRequestBodySize"` // 请求body最大尺寸 TODO 需要实现
|
||||
AccessLogRef *HTTPAccessLogRef `yaml:"accessLog" json:"accessLog"` // 访问日志配置
|
||||
StatRef *HTTPStatRef `yaml:"statRef" json:"statRef"` // 统计配置
|
||||
CacheRef *HTTPCacheRef `yaml:"cacheRef" json:"cacheRef"` // 缓存配置
|
||||
CacheRefs []*HTTPCacheRef `yaml:"cacheRefs" json:"cacheRefs"` // 缓存配置
|
||||
CachePolicies []*HTTPCachePolicy `yaml:"cachePolicies" json:"cachePolicies"` // 缓存策略
|
||||
FirewallRef *HTTPFirewallRef `yaml:"firewallRef" json:"firewallRef"` // 防火墙设置
|
||||
WebsocketRef *HTTPWebsocketRef `yaml:"websocketRef" json:"websocketRef"` // Websocket应用配置
|
||||
Websocket *HTTPWebsocketConfig `yaml:"websocket" json:"websocket"` // Websocket配置
|
||||
@@ -111,8 +112,14 @@ func (this *HTTPWebConfig) Init() error {
|
||||
}
|
||||
|
||||
// cache
|
||||
if this.CacheRef != nil {
|
||||
err := this.CacheRef.Init()
|
||||
for _, cacheRef := range this.CacheRefs {
|
||||
err := cacheRef.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, cachePolicy := range this.CachePolicies {
|
||||
err := cachePolicy.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user