缓存条件增加暂停/恢复功能;缓存条件修改后自动保存

This commit is contained in:
GoEdgeLab
2022-04-08 20:49:31 +08:00
parent 4a7bdc5d64
commit 663512976a
9 changed files with 129 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ func init() {
GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/purge", new(PurgeAction)).
GetPost("/preheat", new(PreheatAction)).
Post("/updateRefs", new(UpdateRefsAction)).
EndAll()
})
}

View File

@@ -0,0 +1,83 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package cache
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
type UpdateRefsAction struct {
actionutils.ParentAction
}
func (this *UpdateRefsAction) RunPost(params struct {
WebId int64
RefsJSON []byte
}) {
// 日志
defer this.CreateLog(oplogs.LevelInfo, "修改Web %d 的缓存设置", params.WebId)
this.Data["isUpdated"] = false
webConfigResp, err := this.RPC().HTTPWebRPC().FindEnabledHTTPWebConfig(this.AdminContext(), &pb.FindEnabledHTTPWebConfigRequest{HttpWebId: params.WebId})
if err != nil {
this.ErrorPage(err)
return
}
var webConfig = &serverconfigs.HTTPWebConfig{}
err = json.Unmarshal(webConfigResp.HttpWebJSON, webConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["cacheConfig"] = webConfig.Cache
// 校验配置
var cacheConfig = webConfig.Cache
if webConfig == nil {
this.Success()
}
var refs = []*serverconfigs.HTTPCacheRef{}
err = json.Unmarshal(params.RefsJSON, &refs)
if err != nil {
this.ErrorPage(errors.New("decode refs json failed: " + err.Error()))
return
}
cacheConfig.CacheRefs = refs
err = cacheConfig.Init()
if err != nil {
this.Fail("检查配置失败:" + err.Error())
}
// 去除不必要的部分
for _, cacheRef := range cacheConfig.CacheRefs {
cacheRef.CachePolicy = nil
}
cacheJSON, err := json.Marshal(cacheConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebCache(this.AdminContext(), &pb.UpdateHTTPWebCacheRequest{
HttpWebId: params.WebId,
CacheJSON: cacheJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["isUpdated"] = true
this.Success()
}