实现缓存策略若干功能

This commit is contained in:
刘祥超
2020-10-04 16:10:19 +08:00
parent 575c59267a
commit 75f653972d
12 changed files with 1077 additions and 472 deletions

View File

@@ -1,6 +1,8 @@
package serverconfigs
import (
"bytes"
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
)
@@ -10,9 +12,9 @@ type HTTPCachePolicy struct {
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"` // 单个缓存最大尺寸
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"` // 类型
Options map[string]interface{} `yaml:"options" json:"options"` // 选项
@@ -34,3 +36,16 @@ func (this *HTTPCachePolicy) Init() error {
func (this *HTTPCachePolicy) CapacitySize() int64 {
return this.capacity
}
// 对比Policy是否有变化
func (this *HTTPCachePolicy) IsSame(anotherPolicy *HTTPCachePolicy) bool {
policyJSON1, err := json.Marshal(this)
if err != nil {
return false
}
policyJSON2, err := json.Marshal(anotherPolicy)
if err != nil {
return false
}
return bytes.Equal(policyJSON1, policyJSON2)
}

View File

@@ -0,0 +1,76 @@
package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestHTTPCachePolicy_IsSame(t *testing.T) {
a := assert.NewAssertion(t)
{
p1 := &HTTPCachePolicy{}
p2 := &HTTPCachePolicy{}
a.IsTrue(p1.IsSame(p2))
}
{
p1 := &HTTPCachePolicy{
Capacity: &shared.SizeCapacity{
Count: 0,
Unit: "",
},
}
p2 := &HTTPCachePolicy{}
a.IsFalse(p1.IsSame(p2))
}
{
p1 := &HTTPCachePolicy{
Capacity: &shared.SizeCapacity{
Count: 0,
Unit: "",
},
}
p2 := &HTTPCachePolicy{
Capacity: &shared.SizeCapacity{
Count: 0,
Unit: "",
},
}
a.IsTrue(p1.IsSame(p2))
}
{
p1 := &HTTPCachePolicy{
Options: map[string]interface{}{},
}
p2 := &HTTPCachePolicy{}
a.IsFalse(p1.IsSame(p2))
}
{
p1 := &HTTPCachePolicy{
Options: map[string]interface{}{},
}
p2 := &HTTPCachePolicy{
Options: map[string]interface{}{},
}
a.IsTrue(p1.IsSame(p2))
}
{
p1 := &HTTPCachePolicy{
Options: map[string]interface{}{
"c": 3,
"a": 1,
"d": "abc",
"b": 2,
},
}
p2 := &HTTPCachePolicy{
Options: map[string]interface{}{
"c": 3,
"a": 1,
"d": "abc",
"b": 2,
},
}
a.IsTrue(p1.IsSame(p2))
}
}

View File

@@ -63,6 +63,20 @@ func (this *NetworkAddressConfig) Init() error {
return nil
}
// 所有的地址列表不包括scheme
func (this *NetworkAddressConfig) Addresses() []string {
if this.Protocol == ProtocolUnix {
return []string{this.Host}
}
result := []string{}
for i := this.minPort; i <= this.maxPort; i++ {
host := this.Host
result = append(result, host+":"+strconv.Itoa(i))
}
return result
}
// 所有的地址列表包含scheme
func (this *NetworkAddressConfig) FullAddresses() []string {
if this.Protocol == ProtocolUnix {