9 changed files with 149 additions and 22 deletions
-
11internal/consts/consts.go
-
17internal/logic/vote_record/add_record.go
-
23internal/logic/vote_record/get_export_file.go
-
10internal/logic/vote_record/get_index.go
-
24internal/logic/vote_record/get_vote.go
-
26internal/logic/vote_record/get_vote_detail.go
-
13internal/utils/cachettl.go
-
45internal/utils/delete_keys.go
-
2main.go
@ -1 +1,12 @@ |
|||||
package consts |
package consts |
||||
|
|
||||
|
const ( |
||||
|
BASE_TTL = 600 |
||||
|
RANDOM_RANGE = 60 |
||||
|
) |
||||
|
|
||||
|
const ( |
||||
|
VOTE_DETAIL = "voteDetail" //投票名单查询
|
||||
|
ARTICLE = "article" //文章查询
|
||||
|
EXPORT_FILE = "exportFile" //导出数据查询
|
||||
|
) |
@ -0,0 +1,13 @@ |
|||||
|
package utils |
||||
|
|
||||
|
import ( |
||||
|
"math/rand" |
||||
|
"practice_ArticleVote_Go/internal/consts" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
func GetCacheTTL() time.Duration { |
||||
|
baseTTL := consts.BASE_TTL |
||||
|
randomNumber := rand.Intn(consts.RANDOM_RANGE) |
||||
|
return time.Duration(baseTTL+randomNumber) * time.Second |
||||
|
} |
@ -0,0 +1,45 @@ |
|||||
|
package utils |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"fmt" |
||||
|
"github.com/gogf/gf/v2/frame/g" |
||||
|
"github.com/gogf/gf/v2/util/gconv" |
||||
|
) |
||||
|
|
||||
|
func DeleteKeys(ctx context.Context, pattern string) (int, error) { |
||||
|
redis := g.Redis() |
||||
|
var ( |
||||
|
cursor uint64 |
||||
|
totalKeys int |
||||
|
batchSize = 100 |
||||
|
) |
||||
|
g.Log().Debugf(ctx, "开始删除匹配模式 %q 的缓存键", pattern) |
||||
|
for { |
||||
|
result, err := redis.Do(ctx, "SCAN", cursor, "MATCH", pattern, "COUNT", batchSize) |
||||
|
if err != nil { |
||||
|
return totalKeys, fmt.Errorf("SCAN 失败: %v", err) |
||||
|
} |
||||
|
arr := result.Array() |
||||
|
fmt.Println("arr:", arr) |
||||
|
cursor = gconv.Uint64(arr[0]) |
||||
|
rawKeys := gconv.Strings(arr[1]) |
||||
|
var keys []string |
||||
|
for _, k := range rawKeys { |
||||
|
keys = append(keys, k) |
||||
|
} |
||||
|
fmt.Println("keys:", keys) |
||||
|
if len(keys) > 0 { |
||||
|
deleted, err := redis.Del(ctx, keys...) |
||||
|
if err != nil { |
||||
|
g.Log().Warningf(ctx, "缓存删除异常: %v", err) |
||||
|
} |
||||
|
totalKeys += int(deleted) |
||||
|
g.Log().Debugf(ctx, "本次删除 %d/%d 个键", deleted, len(keys)) |
||||
|
} |
||||
|
if cursor == 0 { |
||||
|
break |
||||
|
} |
||||
|
} |
||||
|
return totalKeys, nil |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue