2020-09-13 19:27:47 +08:00
|
|
|
|
package serverconfigs
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2020-10-04 16:10:19 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
2020-09-13 19:27:47 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
2023-08-06 17:06:39 +08:00
|
|
|
|
"time"
|
2020-09-13 19:27:47 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-06 17:06:39 +08:00
|
|
|
|
const DefaultHTTPCachePolicyFetchTimeout = 20 * time.Minute
|
|
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
// HTTPCachePolicy 缓存策略配置
|
2020-09-16 09:09:31 +08:00
|
|
|
|
type HTTPCachePolicy struct {
|
2022-02-24 20:13:16 +08:00
|
|
|
|
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"` // 最大内容容量
|
|
|
|
|
|
MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 单个缓存最大尺寸
|
|
|
|
|
|
Type CachePolicyStorageType `yaml:"type" json:"type"` // 类型
|
2023-08-06 17:06:39 +08:00
|
|
|
|
Options map[string]any `yaml:"options" json:"options"` // 选项
|
2022-02-24 20:13:16 +08:00
|
|
|
|
Life *shared.TimeDuration `yaml:"life" json:"life"` // 默认有效期 TODO 需要实现
|
|
|
|
|
|
MinLife *shared.TimeDuration `yaml:"minLife" json:"minLife"` // 最小有效期 TODO 需要实现
|
|
|
|
|
|
MaxLife *shared.TimeDuration `yaml:"maxLife" json:"maxLife"` // 最大有效期 TODO 需要实现
|
|
|
|
|
|
SyncCompressionCache bool `yaml:"syncCompressionCache" json:"syncCompressionCache"` // 同步写入压缩缓存
|
2023-08-06 17:06:39 +08:00
|
|
|
|
FetchTimeout *shared.TimeDuration `yaml:"fetchTimeout" json:"fetchTimeout"` // 预热超时时间
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
CacheRefs []*HTTPCacheRef `yaml:"cacheRefs" json:"cacheRefs"` // 缓存配置
|
|
|
|
|
|
|
2021-11-13 21:02:09 +08:00
|
|
|
|
PersistenceAutoPurgeCount int `yaml:"persistenceAutoPurgeCount" json:"persistenceAutoPurgeCount"` // 每次自动清理的条数 TODO 需要实现
|
|
|
|
|
|
PersistenceAutoPurgeInterval int `yaml:"persistenceAutoPurgeInterval" json:"persistenceAutoPurgeInterval"` // 自动清理的时间间隔(秒) TODO 需要实现
|
|
|
|
|
|
PersistenceLFUFreePercent float32 `yaml:"persistenceLFUFreePercent" json:"persistenceLFUFreePercent"` // LFU算法执行阈值,剩余空间比例,使用百分比,比如20 TODO 需要实现
|
2021-11-14 16:21:31 +08:00
|
|
|
|
PersistenceHitSampleRate int `yaml:"persistenceHitSampleRate" json:"persistenceHitSampleRate"` // 热点数据采样比例 TODO 需要实现
|
2021-11-13 21:02:09 +08:00
|
|
|
|
|
|
|
|
|
|
MemoryAutoPurgeCount int `yaml:"memoryAutoPurgeCount" json:"memoryAutoPurgeCount"` // 每次自动清理的条数 TODO 需要实现
|
|
|
|
|
|
MemoryAutoPurgeInterval int `yaml:"memoryAutoPurgeInterval" json:"memoryAutoPurgeInterval"` // 自动清理的时间间隔(秒) TODO 需要实现
|
|
|
|
|
|
MemoryLFUFreePercent float32 `yaml:"memoryLFUFreePercent" json:"memoryLFUFreePercent"` // LFU算法执行阈值,剩余空间比例,使用百分比,比如20 TODO 需要实现
|
|
|
|
|
|
MemoryAutoFlushQueueSize int `yaml:"memoryAutoFlushQueueSize" json:"memoryAutoFlushQueueSize"` // 自动刷新到持久层队列尺寸 TODO 需要实现
|
|
|
|
|
|
|
2020-09-13 19:27:47 +08:00
|
|
|
|
capacity int64
|
2020-10-05 16:54:21 +08:00
|
|
|
|
maxSize int64
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
// Init 校验
|
2020-09-20 11:56:22 +08:00
|
|
|
|
func (this *HTTPCachePolicy) Init() error {
|
2020-10-02 17:22:46 +08:00
|
|
|
|
if this.Capacity != nil {
|
|
|
|
|
|
this.capacity = this.Capacity.Bytes()
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-11 11:52:13 +08:00
|
|
|
|
if this.MaxSize != nil {
|
2020-10-05 16:54:21 +08:00
|
|
|
|
this.maxSize = this.MaxSize.Bytes()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
for _, cacheRef := range this.CacheRefs {
|
|
|
|
|
|
err := cacheRef.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
// CapacityBytes 容量
|
2020-10-05 20:23:06 +08:00
|
|
|
|
func (this *HTTPCachePolicy) CapacityBytes() int64 {
|
2020-09-13 19:27:47 +08:00
|
|
|
|
return this.capacity
|
|
|
|
|
|
}
|
2020-10-04 16:10:19 +08:00
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
// MaxSizeBytes 单个缓存最大尺寸
|
2020-10-05 16:54:21 +08:00
|
|
|
|
func (this *HTTPCachePolicy) MaxSizeBytes() int64 {
|
|
|
|
|
|
return this.maxSize
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-24 19:07:33 +08:00
|
|
|
|
// IsSame 对比Policy是否有变化
|
2020-10-04 16:10:19 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
2022-03-16 15:24:56 +08:00
|
|
|
|
|
|
|
|
|
|
// UpdateDiskDir 修改文件路径
|
2022-11-15 20:35:39 +08:00
|
|
|
|
func (this *HTTPCachePolicy) UpdateDiskDir(dir string, subDirs []*CacheDir) {
|
2022-03-16 15:24:56 +08:00
|
|
|
|
if this.Type == CachePolicyStorageFile {
|
2022-11-15 20:40:59 +08:00
|
|
|
|
if len(dir) == 0 && len(subDirs) == 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-16 15:24:56 +08:00
|
|
|
|
oldOptionsJSON, err := json.Marshal(this.Options)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-29 18:31:53 +08:00
|
|
|
|
var options = NewHTTPFileCacheStorage()
|
2022-03-16 15:24:56 +08:00
|
|
|
|
err = json.Unmarshal(oldOptionsJSON, options)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-15 20:40:59 +08:00
|
|
|
|
if len(dir) > 0 {
|
|
|
|
|
|
options.Dir = dir
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(subDirs) > 0 {
|
|
|
|
|
|
options.SubDirs = subDirs
|
|
|
|
|
|
}
|
2022-03-16 15:24:56 +08:00
|
|
|
|
|
2022-11-15 20:35:39 +08:00
|
|
|
|
newOptionsJSON, err := json.Marshal(options)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-03-16 15:24:56 +08:00
|
|
|
|
|
2022-11-15 20:35:39 +08:00
|
|
|
|
var newOptionsMap = map[string]any{}
|
|
|
|
|
|
err = json.Unmarshal(newOptionsJSON, &newOptionsMap)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
2022-03-16 15:24:56 +08:00
|
|
|
|
}
|
2022-11-15 20:35:39 +08:00
|
|
|
|
this.Options = newOptionsMap
|
2022-03-16 15:24:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|