mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-04 21:50:28 +08:00
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
|
|
package cacheutils
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/errors"
|
||
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 查找缓存策略名称并忽略错误
|
||
|
|
func FindCachePolicyNameWithoutError(parent *actionutils.ParentAction, cachePolicyId int64) string {
|
||
|
|
policy, err := FindCachePolicy(parent, cachePolicyId)
|
||
|
|
if err != nil {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
if policy == nil {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
return policy.Name
|
||
|
|
}
|
||
|
|
|
||
|
|
// 查找缓存策略配置
|
||
|
|
func FindCachePolicy(parent *actionutils.ParentAction, cachePolicyId int64) (*serverconfigs.HTTPCachePolicy, error) {
|
||
|
|
resp, err := parent.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(parent.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: cachePolicyId})
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if len(resp.CachePolicyJSON) == 0 {
|
||
|
|
return nil, errors.New("cache policy not found")
|
||
|
|
}
|
||
|
|
config := &serverconfigs.HTTPCachePolicy{}
|
||
|
|
err = json.Unmarshal(resp.CachePolicyJSON, config)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return config, nil
|
||
|
|
}
|