refactor: 将布隆过滤器改为Cuckoo过滤器并优化代码结构
All checks were successful
构建上传工具 / build-tool (push) Successful in 59s
部署开发环境 / deploy-dev (push) Successful in 1m36s

This commit is contained in:
2025-09-12 22:26:45 +08:00
parent 5d304b6334
commit 1c56423ea4
4 changed files with 83 additions and 72 deletions

View File

@@ -3,9 +3,9 @@ package controller
import (
"dypid/db"
"dypid/global"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
@@ -116,9 +116,10 @@ func GetTokenInfoHandler(c *gin.Context) {
func DeleteTokenInfoHandler(c *gin.Context) {
//解析输入数据
input := struct {
Token string `form:"token" binding:"required"`
DedupBF string `form:"dedup_bf"`
CacheList string `form:"cache_list"`
Token string `form:"token" binding:"required"`
DedupBF string `form:"dedup_bf"`
CacheList string `form:"cache_list"`
BothNumber string `form:"both_number"`
}{}
if err := c.ShouldBindQuery(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Token不能为空"})
@@ -132,6 +133,12 @@ func DeleteTokenInfoHandler(c *gin.Context) {
return
}
dedupObject, err := db.GetDedupObject(input.Token)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
//删除去重对象
switch input.DedupBF {
case "":
@@ -139,6 +146,7 @@ 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)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "dedup_bf设置错误 " + err.Error()})
@@ -155,9 +163,25 @@ func DeleteTokenInfoHandler(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "cache_list设置错误 " + err.Error()})
return
}
fmt.Println(-i)
global.RDB.LTrim(global.RCtx, "list:"+input.Token, 1, int64(-i))
}
//TODO
switch input.BothNumber {
case "":
default:
i, err := strconv.Atoi(input.BothNumber)
if err != nil {
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)
}
}
//输出信息
c.JSON(http.StatusOK, gin.H{"result": "ok"})