缓存策略增加“缓存磁盘最小空余空间”选项

This commit is contained in:
刘祥超
2023-08-06 18:09:01 +08:00
parent 3c44e14386
commit cc60c827da
6 changed files with 103 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ package utils
import (
"bytes"
"encoding/json"
"errors"
"reflect"
)
@@ -28,3 +29,32 @@ func JSONClone(v interface{}) (interface{}, error) {
func JSONIsNull(jsonData []byte) bool {
return len(jsonData) == 0 || bytes.Equal(jsonData, []byte("null"))
}
// JSONDecodeConfig 解码并重新编码
// 是为了去除原有JSON中不需要的数据
func JSONDecodeConfig(data []byte, ptr any) (encodeJSON []byte, err error) {
err = json.Unmarshal(data, ptr)
if err != nil {
return
}
encodeJSON, err = json.Marshal(ptr)
if err != nil {
return
}
// validate config
if ptr != nil {
config, ok := ptr.(interface {
Init() error
})
if ok {
initErr := config.Init()
if initErr != nil {
err = errors.New("validate config failed: " + initErr.Error())
}
}
}
return
}