mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 04:10:25 +08:00
实现服务的缓存策略设置
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
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))
|
||||
}
|
||||
18
pkg/serverconfigs/http_cache_config.go
Normal file
18
pkg/serverconfigs/http_cache_config.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPCacheConfig struct {
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"`
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
|
||||
CacheRefs []*HTTPCacheRef `yaml:"cacheRefs" json:"cacheRefs"` // 缓存配置
|
||||
}
|
||||
|
||||
func (this *HTTPCacheConfig) Init() error {
|
||||
for _, cacheRef := range this.CacheRefs {
|
||||
err := cacheRef.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package serverconfigs
|
||||
|
||||
type CachePolicyType = string
|
||||
|
||||
type HTTPFileCacheConfig struct {
|
||||
Dir string `yaml:"dir" json:"dir"` // 目录
|
||||
}
|
||||
|
||||
func (this *HTTPFileCacheConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPMemoryCacheConfig struct {
|
||||
}
|
||||
|
||||
func (this *HTTPMemoryCacheConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
@@ -15,7 +15,7 @@ type HTTPCachePolicy struct {
|
||||
Capacity *shared.SizeCapacity `yaml:"capacity" json:"capacity"` // 最大内容容量 TODO 需要实现
|
||||
MaxKeys int64 `yaml:"maxKeys" json:"maxKeys"` // 最多Key值 TODO 需要实现
|
||||
MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 单个缓存最大尺寸 TODO 需要实现
|
||||
Type CachePolicyType `yaml:"type" json:"type"` // 类型
|
||||
Type CachePolicyStorageType `yaml:"type" json:"type"` // 类型
|
||||
Options map[string]interface{} `yaml:"options" json:"options"` // 选项
|
||||
|
||||
capacity int64
|
||||
|
||||
@@ -2,25 +2,27 @@ package serverconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
type CachePolicyStorageType = string
|
||||
|
||||
const (
|
||||
CachePolicyTypeFile CachePolicyType = "file"
|
||||
CachePolicyTypeMemory CachePolicyType = "memory"
|
||||
CachePolicyStorageFile CachePolicyStorageType = "file"
|
||||
CachePolicyStorageMemory CachePolicyStorageType = "memory"
|
||||
)
|
||||
|
||||
var AllCachePolicyTypes = []maps.Map{
|
||||
var AllCachePolicyStorageTypes = []maps.Map{
|
||||
{
|
||||
"name": "文件缓存",
|
||||
"type": CachePolicyTypeFile,
|
||||
"type": CachePolicyStorageFile,
|
||||
},
|
||||
{
|
||||
"name": "内存缓存",
|
||||
"type": CachePolicyTypeMemory,
|
||||
"type": CachePolicyStorageMemory,
|
||||
},
|
||||
}
|
||||
|
||||
// 根据类型查找名称
|
||||
func FindCachePolicyTypeName(policyType CachePolicyType) string {
|
||||
for _, t := range AllCachePolicyTypes {
|
||||
func FindCachePolicyStorageName(policyType CachePolicyStorageType) string {
|
||||
for _, t := range AllCachePolicyStorageTypes {
|
||||
if t.GetString("type") == policyType {
|
||||
return t.GetString("name")
|
||||
}
|
||||
|
||||
@@ -1,18 +1,80 @@
|
||||
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 HTTPCacheRef struct {
|
||||
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"` // 条件
|
||||
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"`
|
||||
|
||||
life time.Duration
|
||||
maxSize int64
|
||||
uppercaseSkipCacheControlValues []string
|
||||
}
|
||||
|
||||
func (this *HTTPCacheRef) Init() error {
|
||||
if this.Cond != nil {
|
||||
err := this.Cond.Init()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// policy
|
||||
if this.CachePolicy != nil {
|
||||
err := this.CachePolicy.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 最大数据尺寸
|
||||
func (this *HTTPCacheRef) MaxDataSize() int64 {
|
||||
return this.maxSize
|
||||
}
|
||||
|
||||
// 生命周期
|
||||
func (this *HTTPCacheRef) LifeDuration() time.Duration {
|
||||
return this.life
|
||||
}
|
||||
|
||||
// 是否包含某个Cache-Control值
|
||||
func (this *HTTPCacheRef) ContainsCacheControl(value string) bool {
|
||||
return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value))
|
||||
}
|
||||
|
||||
9
pkg/serverconfigs/http_cache_storage_file.go
Normal file
9
pkg/serverconfigs/http_cache_storage_file.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPFileCacheStorage struct {
|
||||
Dir string `yaml:"dir" json:"dir"` // 目录
|
||||
}
|
||||
|
||||
func (this *HTTPFileCacheStorage) Init() error {
|
||||
return nil
|
||||
}
|
||||
8
pkg/serverconfigs/http_cache_storage_memory.go
Normal file
8
pkg/serverconfigs/http_cache_storage_memory.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package serverconfigs
|
||||
|
||||
type HTTPMemoryCacheStorage struct {
|
||||
}
|
||||
|
||||
func (this *HTTPMemoryCacheStorage) Init() error {
|
||||
return nil
|
||||
}
|
||||
@@ -17,13 +17,12 @@ type HTTPWebConfig struct {
|
||||
MaxRequestBodySize string `yaml:"maxRequestBodySize" json:"maxRequestBodySize"` // 请求body最大尺寸 TODO 需要实现
|
||||
AccessLogRef *HTTPAccessLogRef `yaml:"accessLog" json:"accessLog"` // 访问日志配置
|
||||
StatRef *HTTPStatRef `yaml:"statRef" json:"statRef"` // 统计配置
|
||||
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配置
|
||||
RewriteRefs []*HTTPRewriteRef `yaml:"rewriteRefs" json:"rewriteRefs"` // 重写规则配置
|
||||
RewriteRules []*HTTPRewriteRule `yaml:"rewriteRules" json:"rewriteRules"` // 重写规则
|
||||
Cache *HTTPCacheConfig `yaml:"cache" json:"cache"`
|
||||
FirewallRef *HTTPFirewallRef `yaml:"firewallRef" json:"firewallRef"` // 防火墙设置
|
||||
WebsocketRef *HTTPWebsocketRef `yaml:"websocketRef" json:"websocketRef"` // Websocket应用配置
|
||||
Websocket *HTTPWebsocketConfig `yaml:"websocket" json:"websocket"` // Websocket配置
|
||||
RewriteRefs []*HTTPRewriteRef `yaml:"rewriteRefs" json:"rewriteRefs"` // 重写规则配置
|
||||
RewriteRules []*HTTPRewriteRule `yaml:"rewriteRules" json:"rewriteRules"` // 重写规则
|
||||
|
||||
RequestHeaderPolicyRef *shared.HTTPHeaderPolicyRef `yaml:"requestHeaderPolicyRef" json:"requestHeaderPolicyRef"` // 请求Header
|
||||
RequestHeaderPolicy *shared.HTTPHeaderPolicy `yaml:"requestHeaderPolicy" json:"requestHeaderPolicy"` // 请求Header策略
|
||||
@@ -112,14 +111,8 @@ func (this *HTTPWebConfig) Init() error {
|
||||
}
|
||||
|
||||
// cache
|
||||
for _, cacheRef := range this.CacheRefs {
|
||||
err := cacheRef.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, cachePolicy := range this.CachePolicies {
|
||||
err := cachePolicy.Init()
|
||||
if this.Cache != nil {
|
||||
err := this.Cache.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user