mirror of
				https://github.com/TeaOSLab/EdgeCommon.git
				synced 2025-11-04 05:00:24 +08:00 
			
		
		
		
	实现缓存策略的部分功能
This commit is contained in:
		@@ -19,6 +19,8 @@ type NodeConfig struct {
 | 
			
		||||
 | 
			
		||||
	// 全局配置
 | 
			
		||||
	GlobalConfig *serverconfigs.GlobalConfig `yaml:"globalConfig" json:"globalConfig"` // 全局配置
 | 
			
		||||
 | 
			
		||||
	cachePolicies []*serverconfigs.HTTPCachePolicy
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 取得当前节点配置单例
 | 
			
		||||
@@ -52,6 +54,35 @@ func ResetNodeConfig(nodeConfig *NodeConfig) {
 | 
			
		||||
	shared.Locker.Unlock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 初始化
 | 
			
		||||
func (this *NodeConfig) Init() error {
 | 
			
		||||
	// servers
 | 
			
		||||
	for _, server := range this.Servers {
 | 
			
		||||
		err := server.Init()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// global config
 | 
			
		||||
	if this.GlobalConfig != nil {
 | 
			
		||||
		err := this.GlobalConfig.Init()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// cache policies
 | 
			
		||||
	this.cachePolicies = []*serverconfigs.HTTPCachePolicy{}
 | 
			
		||||
	for _, server := range this.Servers {
 | 
			
		||||
		if server.Web != nil {
 | 
			
		||||
			this.lookupCachePolicy(server.Web)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据网络地址和协议分组
 | 
			
		||||
func (this *NodeConfig) AvailableGroups() []*serverconfigs.ServerGroup {
 | 
			
		||||
	groupMapping := map[string]*serverconfigs.ServerGroup{} // protocol://addr => Server Group
 | 
			
		||||
@@ -77,24 +108,9 @@ func (this *NodeConfig) AvailableGroups() []*serverconfigs.ServerGroup {
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *NodeConfig) Init() error {
 | 
			
		||||
	// servers
 | 
			
		||||
	for _, server := range this.Servers {
 | 
			
		||||
		err := server.Init()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// global config
 | 
			
		||||
	if this.GlobalConfig != nil {
 | 
			
		||||
		err := this.GlobalConfig.Init()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
// 获取使用的所有的缓存策略
 | 
			
		||||
func (this *NodeConfig) AllCachePolicies() []*serverconfigs.HTTPCachePolicy {
 | 
			
		||||
	return this.cachePolicies
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 写入到文件
 | 
			
		||||
@@ -109,3 +125,31 @@ func (this *NodeConfig) Save() error {
 | 
			
		||||
 | 
			
		||||
	return ioutil.WriteFile(Tea.ConfigFile("node.json"), data, 0777)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 查找Web中的缓存策略
 | 
			
		||||
func (this *NodeConfig) lookupCachePolicy(web *serverconfigs.HTTPWebConfig) {
 | 
			
		||||
	if web == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if web.Cache != nil && len(web.Cache.CacheRefs) > 0 {
 | 
			
		||||
		for _, cacheRef := range web.Cache.CacheRefs {
 | 
			
		||||
			if cacheRef.CachePolicy != nil && !this.hasCachePolicy(cacheRef.CachePolicyId) {
 | 
			
		||||
				this.cachePolicies = append(this.cachePolicies, cacheRef.CachePolicy)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, location := range web.Locations {
 | 
			
		||||
		this.lookupCachePolicy(location.Web)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 检查缓存策略是否已收集
 | 
			
		||||
func (this *NodeConfig) hasCachePolicy(cachePolicyId int64) bool {
 | 
			
		||||
	for _, cachePolicy := range this.cachePolicies {
 | 
			
		||||
		if cachePolicy.Id == cachePolicyId {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,8 +17,12 @@ type HTTPCachePolicy struct {
 | 
			
		||||
	MaxSize     *shared.SizeCapacity   `yaml:"maxSize" json:"maxSize"`         // 单个缓存最大尺寸 TODO 需要实现
 | 
			
		||||
	Type        CachePolicyStorageType `yaml:"type" json:"type"`               // 类型
 | 
			
		||||
	Options     map[string]interface{} `yaml:"options" json:"options"`         // 选项
 | 
			
		||||
	Life        *shared.TimeDuration   `yaml:"life" json:"life"`               // 默认有效期 TODO 需要实现
 | 
			
		||||
	MinLife     *shared.TimeDuration   `yaml:"minLife" json:"minLife"`         // 最小有效期 TODO 需要实现
 | 
			
		||||
	MaxLife     *shared.TimeDuration   `yaml:"maxLife" json:"maxLife"`         // 最大有效期 TODO 需要实现
 | 
			
		||||
 | 
			
		||||
	capacity int64
 | 
			
		||||
	maxSize  int64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 校验
 | 
			
		||||
@@ -29,6 +33,10 @@ func (this *HTTPCachePolicy) Init() error {
 | 
			
		||||
		this.capacity = this.Capacity.Bytes()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if this.maxSize > 0 {
 | 
			
		||||
		this.maxSize = this.MaxSize.Bytes()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -37,6 +45,11 @@ func (this *HTTPCachePolicy) CapacitySize() int64 {
 | 
			
		||||
	return this.capacity
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 单个缓存最大尺寸
 | 
			
		||||
func (this *HTTPCachePolicy) MaxSizeBytes() int64 {
 | 
			
		||||
	return this.maxSize
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 对比Policy是否有变化
 | 
			
		||||
func (this *HTTPCachePolicy) IsSame(anotherPolicy *HTTPCachePolicy) bool {
 | 
			
		||||
	policyJSON1, err := json.Marshal(this)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
 | 
			
		||||
	"github.com/iwind/TeaGo/lists"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"}
 | 
			
		||||
@@ -26,7 +25,7 @@ type HTTPCacheRef struct {
 | 
			
		||||
 | 
			
		||||
	CachePolicy *HTTPCachePolicy `yaml:"cachePolicy" json:"cachePolicy"`
 | 
			
		||||
 | 
			
		||||
	life                            time.Duration
 | 
			
		||||
	lifeSeconds                     int64
 | 
			
		||||
	maxSize                         int64
 | 
			
		||||
	uppercaseSkipCacheControlValues []string
 | 
			
		||||
}
 | 
			
		||||
@@ -36,7 +35,7 @@ func (this *HTTPCacheRef) Init() error {
 | 
			
		||||
		this.maxSize = this.MaxSize.Bytes()
 | 
			
		||||
	}
 | 
			
		||||
	if this.Life != nil {
 | 
			
		||||
		this.life = this.Life.Duration()
 | 
			
		||||
		this.lifeSeconds = int64(this.Life.Duration().Seconds())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// control-values
 | 
			
		||||
@@ -65,13 +64,13 @@ func (this *HTTPCacheRef) Init() error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 最大数据尺寸
 | 
			
		||||
func (this *HTTPCacheRef) MaxDataSize() int64 {
 | 
			
		||||
func (this *HTTPCacheRef) MaxSizeBytes() int64 {
 | 
			
		||||
	return this.maxSize
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 生命周期
 | 
			
		||||
func (this *HTTPCacheRef) LifeDuration() time.Duration {
 | 
			
		||||
	return this.life
 | 
			
		||||
func (this *HTTPCacheRef) LifeSeconds() int64 {
 | 
			
		||||
	return this.lifeSeconds
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 是否包含某个Cache-Control值
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ func (this *HTTPRequestCondGroup) Init() error {
 | 
			
		||||
	if len(this.Connector) == 0 {
 | 
			
		||||
		this.Connector = "or"
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	if len(this.Conds) > 0 {
 | 
			
		||||
		for _, cond := range this.Conds {
 | 
			
		||||
			err := cond.Init()
 | 
			
		||||
@@ -43,6 +43,14 @@ func (this *HTTPRequestCondGroup) MatchResponse(formatter func(source string) st
 | 
			
		||||
	return this.match(this.responseConds, formatter)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequestCondGroup) HasRequestConds() bool {
 | 
			
		||||
	return len(this.requestConds) > 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequestCondGroup) HasResponseConds() bool {
 | 
			
		||||
	return len(this.responseConds) > 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPRequestCondGroup) match(conds []*HTTPRequestCond, formatter func(source string) string) bool {
 | 
			
		||||
	if !this.IsOn || len(conds) == 0 {
 | 
			
		||||
		return !this.IsReverse
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,9 @@ type HTTPRequestCondsConfig struct {
 | 
			
		||||
	IsOn      bool                    `yaml:"isOn" json:"isOn"`
 | 
			
		||||
	Connector string                  `yaml:"connector" json:"connector"`
 | 
			
		||||
	Groups    []*HTTPRequestCondGroup `yaml:"groups" json:"groups"`
 | 
			
		||||
 | 
			
		||||
	hasRequestConds  bool
 | 
			
		||||
	hasResponseConds bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 初始化
 | 
			
		||||
@@ -21,6 +24,18 @@ func (this *HTTPRequestCondsConfig) Init() error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 是否有请求条件
 | 
			
		||||
	for _, group := range this.Groups {
 | 
			
		||||
		if group.IsOn {
 | 
			
		||||
			if group.HasRequestConds() {
 | 
			
		||||
				this.hasRequestConds = true
 | 
			
		||||
			}
 | 
			
		||||
			if group.HasResponseConds() {
 | 
			
		||||
				this.hasResponseConds = true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -67,3 +82,13 @@ func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) strin
 | 
			
		||||
	}
 | 
			
		||||
	return ok
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 判断是否有请求条件
 | 
			
		||||
func (this *HTTPRequestCondsConfig) HasRequestConds() bool {
 | 
			
		||||
	return this.hasRequestConds
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 判断是否有响应条件
 | 
			
		||||
func (this *HTTPRequestCondsConfig) HasResponseConds() bool {
 | 
			
		||||
	return this.hasResponseConds
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user