mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-01-02 00:56:35 +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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user