2020-10-04 20:38:27 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var SharedHTTPCachePolicyDAO = new(HTTPCachePolicyDAO)
|
|
|
|
|
|
|
|
|
|
type HTTPCachePolicyDAO struct {
|
2020-12-17 15:50:44 +08:00
|
|
|
BaseDAO
|
2020-10-04 20:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找缓存策略配置
|
2020-12-17 15:50:44 +08:00
|
|
|
func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicyConfig(ctx context.Context, cachePolicyId int64) (*serverconfigs.HTTPCachePolicy, error) {
|
|
|
|
|
resp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(ctx, &pb.FindEnabledHTTPCachePolicyConfigRequest{HttpCachePolicyId: cachePolicyId})
|
2020-10-04 20:38:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-17 15:50:44 +08:00
|
|
|
if len(resp.HttpCachePolicyJSON) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
config := &serverconfigs.HTTPCachePolicy{}
|
|
|
|
|
err = json.Unmarshal(resp.HttpCachePolicyJSON, config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找缓存策略信息
|
|
|
|
|
func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(ctx context.Context, cachePolicyId int64) (*pb.HTTPCachePolicy, error) {
|
|
|
|
|
resp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicy(ctx, &pb.FindEnabledHTTPCachePolicyRequest{
|
|
|
|
|
HttpCachePolicyId: cachePolicyId,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return resp.HttpCachePolicy, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据服务ID查找缓存策略
|
|
|
|
|
func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicyWithServerId(ctx context.Context, serverId int64) (*pb.HTTPCachePolicy, error) {
|
|
|
|
|
serverResp, err := this.RPC().ServerRPC().FindEnabledServer(ctx, &pb.FindEnabledServerRequest{ServerId: serverId})
|
2020-10-04 20:38:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-17 15:50:44 +08:00
|
|
|
server := serverResp.Server
|
|
|
|
|
if server == nil {
|
2020-10-04 20:38:27 +08:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-12-17 17:35:38 +08:00
|
|
|
if server.NodeCluster == nil {
|
2020-12-17 15:50:44 +08:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-12-17 17:35:38 +08:00
|
|
|
clusterId := server.NodeCluster.Id
|
2020-12-17 15:50:44 +08:00
|
|
|
cluster, err := SharedNodeClusterDAO.FindEnabledNodeCluster(ctx, clusterId)
|
2020-10-04 20:38:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-17 15:50:44 +08:00
|
|
|
if cluster == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
if cluster.HttpCachePolicyId == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return SharedHTTPCachePolicyDAO.FindEnabledHTTPCachePolicy(ctx, cluster.HttpCachePolicyId)
|
2020-10-04 20:38:27 +08:00
|
|
|
}
|