refactor(controller): 优化Redis去重和缓存删除逻辑
All checks were successful
构建上传工具 / build-tool (push) Successful in 56s
部署开发环境 / deploy-dev (push) Successful in 1m39s

This commit is contained in:
2025-09-14 22:51:04 +08:00
parent 4c19f4b16c
commit 78d496ae72
3 changed files with 80 additions and 12 deletions

View File

@@ -5,9 +5,9 @@ import (
"dypid/global"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)
func ListTokenHandler(c *gin.Context) {
@@ -146,17 +146,29 @@ func DeleteTokenInfoHandler(c *gin.Context) {
keys := global.RDB.Keys(global.RCtx, "dedup:"+input.Token+":*").Val()
global.RDB.Del(global.RCtx, keys...)
default:
//TODO 不考虑单独删除指定数量去重参考值
_, err := strconv.Atoi(input.DedupBF)
i, err := strconv.Atoi(input.DedupBF)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "dedup_bf设置错误 " + err.Error()})
return
}
result := global.RDB.LRange(global.RCtx, "delete-list:"+input.Token, 1, int64(i)).Val()
_, err = global.RDB.TxPipelined(global.RCtx, func(pipe redis.Pipeliner) error {
for _, s := range result {
pipe.CFDel(global.RCtx, "dedup:"+input.Token+":"+dedupObject, s)
}
return nil
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
global.RDB.LTrim(global.RCtx, "delete-list:"+input.Token, int64(i), -1)
}
switch input.CacheList {
case "":
case "all":
global.RDB.Del(global.RCtx, "list:"+input.Token)
global.RDB.Del(global.RCtx, "delete-list:"+input.Token)
default:
i, err := strconv.Atoi(input.CacheList)
if err != nil {
@@ -174,13 +186,19 @@ func DeleteTokenInfoHandler(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "both_number设置错误 " + err.Error()})
return
}
result := global.RDB.LRange(global.RCtx, "list:"+input.Token, 1, int64(i)).Val()
global.RDB.LTrim(global.RCtx, "list:"+input.Token, 1, int64(-i))
dataIndex, err := getDataIndex(input.Token)
for _, s := range result {
s2 := strings.Split(s, "----")[dataIndex[dedupObject]]
global.RDB.CFDel(global.RCtx, "dedup:"+input.Token+":"+dedupObject, s2)
result := global.RDB.LRange(global.RCtx, "delete-list:"+input.Token, 1, int64(i)).Val()
_, err = global.RDB.TxPipelined(global.RCtx, func(pipe redis.Pipeliner) error {
for _, s := range result {
pipe.CFDel(global.RCtx, "dedup:"+input.Token+":"+dedupObject, s)
}
return nil
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
global.RDB.LTrim(global.RCtx, "delete-list:"+input.Token, int64(i), -1)
global.RDB.LTrim(global.RCtx, "list:"+input.Token, int64(i), -1)
}
//输出信息