feat: 优化Redis上传操作 添加文件上传工具

使用Lua脚本确保Redis数据检测重复与插入的原子化
This commit is contained in:
2025-09-01 13:55:56 +08:00
parent b22b4786e6
commit b2c643cf73
6 changed files with 270 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)
func ReadDataHandler(c *gin.Context) {
@@ -43,17 +44,55 @@ func WriteDataHandler(c *gin.Context) {
return
}
exists := global.RDB.BFExists(global.RCtx, fmt.Sprintf("dedup:%s:%s", data.Token, dedupObject), c.Query(dedupObject))
if exists.Val() {
return
}
marshal, err := json.Marshal(data)
luaScript := `
local dedupKey = KEYS[1] -- KEYS[1]: 去重键 (dedup:token:object)
local listKey = KEYS[2] -- KEYS[2]: 列表键 (list:token)
local dedupValue = ARGV[1] -- ARGV[1]: 去重值
local jsonData = ARGV[2] -- ARGV[2]: JSON序列化的数据
-- 检查布隆过滤器中是否已存在该值
local exists = redis.call('BF.EXISTS', dedupKey, dedupValue)
-- 如果已存在,返回已去重标记
if exists == 1 then
return "已去重"
end
-- 添加到布隆过滤器
redis.call('BF.ADD', dedupKey, dedupValue)
-- 添加到列表
redis.call('LPUSH', listKey, jsonData)
-- 返回成功结果
return "ok"
`
k1 := fmt.Sprintf("dedup:%s:%s", data.Token, dedupObject)
k2 := fmt.Sprintf("list:%s", data.Token)
v1 := c.Query(dedupObject)
v2, err := json.Marshal(data)
if err != nil {
c.JSON(500, gin.H{"error": "JSON序列化失败 " + err.Error()})
return
}
global.RDB.LPush(global.RCtx, fmt.Sprintf("list:%s", data.Token), marshal)
global.RDB.BFAdd(global.RCtx, fmt.Sprintf("dedup:%s:%s", data.Token, dedupObject), c.Query(dedupObject))
c.JSON(200, gin.H{"result": "ok"})
result, err := redis.NewScript(luaScript).Run(
global.RCtx,
global.RDB,
[]string{k1, k2},
v1,
string(v2),
).Result()
if err != nil {
c.JSON(500, gin.H{"error": "Redis操作失败 " + err.Error()})
return
}
if resultMap, ok := result.(string); ok {
c.JSON(200, gin.H{"result": resultMap})
return
}
c.JSON(500, gin.H{"error": "WriteDataHandler 错误"})
}
func createBF(bloomFilter string, errorRate float64, capacity int64) error {