Skip to content

Commit f26bc33

Browse files
committed
缓存策略可以设置全局的缓存条件
1 parent 05c2955 commit f26bc33

4 files changed

Lines changed: 64 additions & 21 deletions

File tree

internal/const/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ const (
2222
UserNodeVersion = "0.0.7"
2323
AuthorityNodeVersion = "0.0.1"
2424
MonitorNodeVersion = "0.0.1"
25+
DNSNodeVersion = "0.0.1"
2526
)

internal/db/models/http_cache_policy_dao.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func init() {
3737
})
3838
}
3939

40-
// 初始化
40+
// Init 初始化
4141
func (this *HTTPCachePolicyDAO) Init() {
4242
_ = this.DAOObject.Init()
4343
}
4444

45-
// 启用条目
45+
// EnableHTTPCachePolicy 启用条目
4646
func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) error {
4747
_, err := this.Query(tx).
4848
Pk(id).
@@ -51,7 +51,7 @@ func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) erro
5151
return err
5252
}
5353

54-
// 禁用条目
54+
// DisableHTTPCachePolicy 禁用条目
5555
func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int64) error {
5656
_, err := this.Query(tx).
5757
Pk(policyId).
@@ -63,7 +63,7 @@ func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int6
6363
return this.NotifyUpdate(tx, policyId)
6464
}
6565

66-
// 查找启用中的条目
66+
// FindEnabledHTTPCachePolicy 查找启用中的条目
6767
func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64) (*HTTPCachePolicy, error) {
6868
result, err := this.Query(tx).
6969
Pk(id).
@@ -75,15 +75,15 @@ func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64)
7575
return result.(*HTTPCachePolicy), err
7676
}
7777

78-
// 根据主键查找名称
78+
// FindHTTPCachePolicyName 根据主键查找名称
7979
func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (string, error) {
8080
return this.Query(tx).
8181
Pk(id).
8282
Result("name").
8383
FindStringCol("")
8484
}
8585

86-
// 查找所有可用的缓存策略
86+
// FindAllEnabledCachePolicies 查找所有可用的缓存策略
8787
func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result []*HTTPCachePolicy, err error) {
8888
_, err = this.Query(tx).
8989
State(HTTPCachePolicyStateEnabled).
@@ -93,7 +93,7 @@ func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result
9393
return
9494
}
9595

96-
// 创建缓存策略
96+
// CreateCachePolicy 创建缓存策略
9797
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) (int64, error) {
9898
op := NewHTTPCachePolicyOperator()
9999
op.State = HTTPCachePolicyStateEnabled
@@ -118,7 +118,7 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
118118
return types.Int64(op.Id), nil
119119
}
120120

121-
// 修改缓存策略
121+
// UpdateCachePolicy 修改缓存策略
122122
func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) error {
123123
if policyId <= 0 {
124124
return errors.New("invalid policyId")
@@ -147,7 +147,7 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
147147
return this.NotifyUpdate(tx, policyId)
148148
}
149149

150-
// 组合配置
150+
// ComposeCachePolicy 组合配置
151151
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPCachePolicy, error) {
152152
policy, err := this.FindEnabledHTTPCachePolicy(tx, policyId)
153153
if err != nil {
@@ -196,17 +196,27 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (
196196
config.Options = m
197197
}
198198

199+
// refs
200+
if IsNotNull(policy.Refs) {
201+
refs := []*serverconfigs.HTTPCacheRef{}
202+
err = json.Unmarshal([]byte(policy.Refs), &refs)
203+
if err != nil {
204+
return nil, err
205+
}
206+
config.CacheRefs = refs
207+
}
208+
199209
return config, nil
200210
}
201211

202-
// 计算可用缓存策略数量
212+
// CountAllEnabledHTTPCachePolicies 计算可用缓存策略数量
203213
func (this *HTTPCachePolicyDAO) CountAllEnabledHTTPCachePolicies(tx *dbs.Tx) (int64, error) {
204214
return this.Query(tx).
205215
State(HTTPCachePolicyStateEnabled).
206216
Count()
207217
}
208218

209-
// 列出单页的缓存策略
219+
// ListEnabledHTTPCachePolicies 列出单页的缓存策略
210220
func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset int64, size int64) ([]*serverconfigs.HTTPCachePolicy, error) {
211221
ones, err := this.Query(tx).
212222
State(HTTPCachePolicyStateEnabled).
@@ -237,7 +247,22 @@ func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset
237247
return cachePolicies, nil
238248
}
239249

240-
// 通知更新
250+
// UpdatePolicyRefs 设置默认的缓存条件
251+
func (this *HTTPCachePolicyDAO) UpdatePolicyRefs(tx *dbs.Tx, policyId int64, refsJSON []byte) error {
252+
if len(refsJSON) == 0 {
253+
return nil
254+
}
255+
err := this.Query(tx).
256+
Pk(policyId).
257+
Set("refs", refsJSON).
258+
UpdateQuickly()
259+
if err != nil {
260+
return err
261+
}
262+
return this.NotifyUpdate(tx, policyId)
263+
}
264+
265+
// NotifyUpdate 通知更新
241266
func (this *HTTPCachePolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
242267
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithCachePolicyId(tx, policyId)
243268
if err != nil {

internal/db/models/http_cache_policy_model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package models
22

3-
// HTTP缓存策略
3+
// HTTPCachePolicy HTTP缓存策略
44
type HTTPCachePolicy struct {
55
Id uint32 `field:"id"` // ID
66
AdminId uint32 `field:"adminId"` // 管理员ID
@@ -16,6 +16,7 @@ type HTTPCachePolicy struct {
1616
CreatedAt uint64 `field:"createdAt"` // 创建时间
1717
State uint8 `field:"state"` // 状态
1818
Description string `field:"description"` // 描述
19+
Refs string `field:"refs"` // 默认的缓存设置
1920
}
2021

2122
type HTTPCachePolicyOperator struct {
@@ -33,6 +34,7 @@ type HTTPCachePolicyOperator struct {
3334
CreatedAt interface{} // 创建时间
3435
State interface{} // 状态
3536
Description interface{} // 描述
37+
Refs interface{} // 默认的缓存设置
3638
}
3739

3840
func NewHTTPCachePolicyOperator() *HTTPCachePolicyOperator {

internal/rpc/services/service_http_cache_policy.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type HTTPCachePolicyService struct {
1212
BaseService
1313
}
1414

15-
// 获取所有可用策略
15+
// FindAllEnabledHTTPCachePolicies 获取所有可用策略
1616
func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.FindAllEnabledHTTPCachePoliciesRequest) (*pb.FindAllEnabledHTTPCachePoliciesResponse, error) {
1717
// 校验请求
1818
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -37,7 +37,7 @@ func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context.
3737
return &pb.FindAllEnabledHTTPCachePoliciesResponse{CachePolicies: result}, nil
3838
}
3939

40-
// 创建缓存策略
40+
// CreateHTTPCachePolicy 创建缓存策略
4141
func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, req *pb.CreateHTTPCachePolicyRequest) (*pb.CreateHTTPCachePolicyResponse, error) {
4242
// 校验请求
4343
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -54,7 +54,7 @@ func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, r
5454
return &pb.CreateHTTPCachePolicyResponse{HttpCachePolicyId: policyId}, nil
5555
}
5656

57-
// 修改缓存策略
57+
// UpdateHTTPCachePolicy 修改缓存策略
5858
func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, req *pb.UpdateHTTPCachePolicyRequest) (*pb.RPCSuccess, error) {
5959
// 校验请求
6060
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -72,7 +72,7 @@ func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, r
7272
return this.Success()
7373
}
7474

75-
// 删除缓存策略
75+
// DeleteHTTPCachePolicy 删除缓存策略
7676
func (this *HTTPCachePolicyService) DeleteHTTPCachePolicy(ctx context.Context, req *pb.DeleteHTTPCachePolicyRequest) (*pb.RPCSuccess, error) {
7777
// 校验请求
7878
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -90,7 +90,7 @@ func (this *HTTPCachePolicyService) DeleteHTTPCachePolicy(ctx context.Context, r
9090
return this.Success()
9191
}
9292

93-
// 计算缓存策略数量
93+
// CountAllEnabledHTTPCachePolicies 计算缓存策略数量
9494
func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.CountAllEnabledHTTPCachePoliciesRequest) (*pb.RPCCountResponse, error) {
9595
// 校验请求
9696
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -107,7 +107,7 @@ func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context
107107
return this.SuccessCount(count)
108108
}
109109

110-
// 列出单页的缓存策略
110+
// ListEnabledHTTPCachePolicies 列出单页的缓存策略
111111
func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Context, req *pb.ListEnabledHTTPCachePoliciesRequest) (*pb.ListEnabledHTTPCachePoliciesResponse, error) {
112112
// 校验请求
113113
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -128,7 +128,7 @@ func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Con
128128
return &pb.ListEnabledHTTPCachePoliciesResponse{HttpCachePoliciesJSON: cachePoliciesJSON}, nil
129129
}
130130

131-
// 查找单个缓存策略配置
131+
// FindEnabledHTTPCachePolicyConfig 查找单个缓存策略配置
132132
func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyConfigRequest) (*pb.FindEnabledHTTPCachePolicyConfigResponse, error) {
133133
// 校验请求
134134
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
@@ -146,7 +146,7 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context
146146
return &pb.FindEnabledHTTPCachePolicyConfigResponse{HttpCachePolicyJSON: cachePolicyJSON}, nil
147147
}
148148

149-
// 查找单个缓存策略信息
149+
// FindEnabledHTTPCachePolicy 查找单个缓存策略信息
150150
func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyRequest) (*pb.FindEnabledHTTPCachePolicyResponse, error) {
151151
_, err := this.ValidateAdmin(ctx, 0)
152152
if err != nil {
@@ -168,3 +168,18 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Conte
168168
IsOn: policy.IsOn == 1,
169169
}}, nil
170170
}
171+
172+
// UpdateHTTPCachePolicyRefs 设置缓存策略的默认条件
173+
func (this *HTTPCachePolicyService) UpdateHTTPCachePolicyRefs(ctx context.Context, req *pb.UpdateHTTPCachePolicyRefsRequest) (*pb.RPCSuccess, error) {
174+
_, err := this.ValidateAdmin(ctx, 0)
175+
if err != nil {
176+
return nil, err
177+
}
178+
179+
tx := this.NullTx()
180+
err = models.SharedHTTPCachePolicyDAO.UpdatePolicyRefs(tx, req.HttpCachePolicyId, req.RefsJSON)
181+
if err != nil {
182+
return nil, err
183+
}
184+
return this.Success()
185+
}

0 commit comments

Comments
 (0)