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

@@ -31,6 +31,7 @@ func ReadDataHandler(c *gin.Context) {
}
func WriteDataHandler(c *gin.Context) {
//解析输入数据
input := struct {
Token string `form:"token" binding:"required"`
Data string `form:"data" binding:"required"`
@@ -40,6 +41,7 @@ func WriteDataHandler(c *gin.Context) {
return
}
//数据获取
dedupObject, err := db.GetDedupObject(input.Token)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@@ -52,7 +54,7 @@ func WriteDataHandler(c *gin.Context) {
}
dedupValue := strings.Split(input.Data, "----")[dataIndex[dedupObject]]
err = createBF(fmt.Sprintf("dedup:%s:%s", input.Token, dedupObject), 0.01, 100000000)
err = createCF(fmt.Sprintf("dedup:%s:%s", input.Token, dedupObject), 100000000)
if err != nil && err.Error() != "ERR item exists" {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
@@ -64,7 +66,7 @@ local dedupValue = ARGV[1] -- ARGV[1]: 去重值
local rawData = ARGV[2] -- ARGV[2]: 原始数据
-- 检查布隆过滤器中是否已存在该值
local exists = redis.call('BF.EXISTS', dedupKey, dedupValue)
local exists = redis.call('CF.EXISTS', dedupKey, dedupValue)
-- 如果已存在,返回已去重标记
if exists == 1 then
@@ -72,7 +74,7 @@ if exists == 1 then
end
-- 添加到布隆过滤器
redis.call('BF.ADD', dedupKey, dedupValue)
redis.call('CF.ADD', dedupKey, dedupValue)
-- 添加到列表
redis.call('LPUSH', listKey, rawData)
@@ -102,8 +104,8 @@ return "ok"
c.JSON(http.StatusInternalServerError, gin.H{"error": "WriteDataHandler 错误"})
}
func createBF(bloomFilter string, errorRate float64, capacity int64) error {
_, err := global.RDB.BFReserve(global.RCtx, bloomFilter, errorRate, capacity).Result()
func createCF(bloomFilter string, capacity int64) error {
_, err := global.RDB.CFReserve(global.RCtx, bloomFilter, capacity).Result()
return err
}