refactor(controller): 优化Redis去重和缓存删除逻辑
This commit is contained in:
@@ -54,7 +54,7 @@ func WriteDataHandler(c *gin.Context) {
|
||||
}
|
||||
dedupValue := strings.Split(input.Data, "----")[dataIndex[dedupObject]]
|
||||
|
||||
err = createCF(fmt.Sprintf("dedup:%s:%s", input.Token, dedupObject), 100000000)
|
||||
err = createCF(fmt.Sprintf("dedup:%s:%s", input.Token, dedupObject), 100_000_000)
|
||||
if err != nil && err.Error() != "ERR item exists" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -62,6 +62,7 @@ func WriteDataHandler(c *gin.Context) {
|
||||
luaScript := `
|
||||
local dedupKey = KEYS[1] -- KEYS[1]: 去重键 (dedup:token:object)
|
||||
local listKey = KEYS[2] -- KEYS[2]: 列表键 (list:token)
|
||||
local deleteListKey = KEYS[3] -- KEYS[3]: 删除列表键 (delete-list:token)
|
||||
local dedupValue = ARGV[1] -- ARGV[1]: 去重值
|
||||
local rawData = ARGV[2] -- ARGV[2]: 原始数据
|
||||
|
||||
@@ -78,6 +79,8 @@ redis.call('CF.ADD', dedupKey, dedupValue)
|
||||
-- 添加到列表
|
||||
redis.call('LPUSH', listKey, rawData)
|
||||
|
||||
redis.call('LPUSH', deleteListKey, dedupValue)
|
||||
|
||||
-- 返回成功结果
|
||||
return "ok"
|
||||
`
|
||||
@@ -87,6 +90,7 @@ return "ok"
|
||||
[]string{
|
||||
fmt.Sprintf("dedup:%s:%s", input.Token, dedupObject),
|
||||
fmt.Sprintf("list:%s", input.Token),
|
||||
fmt.Sprintf("delete-list:%s", input.Token),
|
||||
},
|
||||
dedupValue,
|
||||
input.Data,
|
||||
@@ -105,7 +109,7 @@ return "ok"
|
||||
}
|
||||
|
||||
func createCF(bloomFilter string, capacity int64) error {
|
||||
_, err := global.RDB.CFReserve(global.RCtx, bloomFilter, capacity).Result()
|
||||
_, err := global.RDB.CFReserveBucketSize(global.RCtx, bloomFilter, capacity, 6).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user