Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 2839713

Browse files
committed
[cache]清理所有缓存时,会将此动作同步到集群
1 parent e61a40b commit 2839713

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

teacluster/action_run.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func (this *RunAction) Execute() error {
2323
switch this.Cmd {
2424
case "cache.refresh":
2525
this.runCacheRefresh()
26+
case "cache.clean":
27+
this.runCacheClean()
2628
}
2729
return nil
2830
}
@@ -43,6 +45,9 @@ func (this *RunAction) runCacheRefresh() {
4345
manager := teacache.FindCachePolicyManager(filename)
4446
if manager == nil {
4547
manager = teacache.NewManagerFromConfig(policy)
48+
defer func() {
49+
_ = manager.Close()
50+
}()
4651
}
4752
prefixes := this.Data.GetSlice("prefixes")
4853
prefixStrings := []string{}
@@ -51,7 +56,30 @@ func (this *RunAction) runCacheRefresh() {
5156
}
5257
_, err := manager.DeletePrefixes(prefixStrings)
5358
if err != nil {
54-
logs.Println("[cluster][cache.refresh]delete prefixes")
59+
logs.Println("[cluster][cache.refresh]delete prefixes: " + err.Error())
60+
return
61+
}
62+
}
63+
64+
// clean cache
65+
func (this *RunAction) runCacheClean() {
66+
filename := this.Data.GetString("filename")
67+
policy := shared.NewCachePolicyFromFile(filename)
68+
if policy == nil {
69+
logs.Println("[cluster][cache.clean]can not find policy with '" + filename + "'")
70+
return
71+
}
72+
73+
manager := teacache.FindCachePolicyManager(filename)
74+
if manager == nil {
75+
manager = teacache.NewManagerFromConfig(policy)
76+
defer func() {
77+
_ = manager.Close()
78+
}()
79+
}
80+
err := manager.Clean()
81+
if err != nil {
82+
logs.Println("[cluster][cache.clean]clean: " + err.Error())
5583
return
5684
}
5785
}

teaweb/actions/default/cache/cleanPolicy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cache
22

33
import (
44
"github.com/TeaWeb/code/teacache"
5+
"github.com/TeaWeb/code/teacluster"
56
"github.com/TeaWeb/code/teaconfigs/shared"
67
"github.com/TeaWeb/code/teaweb/actions/default/actionutils"
8+
"github.com/iwind/TeaGo/maps"
79
)
810

911
type CleanPolicyAction struct {
@@ -53,6 +55,17 @@ func (this *CleanPolicyAction) RunPost(params struct {
5355
this.Fail()
5456
}
5557

58+
// 同步到集群
59+
action := new(teacluster.RunAction)
60+
action.Cmd = "cache.clean"
61+
action.Data = maps.Map{
62+
"filename": params.Filename,
63+
}
64+
err = teacluster.SharedManager.Write(action)
65+
if err != nil {
66+
this.Fail("同步到集群失败:" + err.Error())
67+
}
68+
5669
this.Data["result"] = "清理完成"
5770
this.Success()
5871
}

teaweb/actions/default/cache/refreshPolicy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (this *RefreshPolicyAction) RunPost(params struct {
7575

7676
this.Data["count"] = count
7777

78-
// 清除节点
78+
// 同步到集群
7979
action := new(teacluster.RunAction)
8080
action.Cmd = "cache.refresh"
8181
action.Data = maps.Map{
@@ -84,7 +84,7 @@ func (this *RefreshPolicyAction) RunPost(params struct {
8484
}
8585
err = teacluster.SharedManager.Write(action)
8686
if err != nil {
87-
this.Fail("刷新集群失败:" + err.Error())
87+
this.Fail("同步到集群失败:" + err.Error())
8888
}
8989

9090
this.Success()

0 commit comments

Comments
 (0)