refactor(data): 优化数据读取和删除逻辑
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func ReadDataHandler(c *gin.Context) {
|
||||
//解析输入数据
|
||||
input := struct {
|
||||
Token string `form:"token" binding:"required"`
|
||||
}{}
|
||||
@@ -20,12 +21,11 @@ func ReadDataHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
lLen := global.RDB.LLen(global.RCtx, fmt.Sprintf("list:%s", input.Token))
|
||||
if lLen.Val() == 0 {
|
||||
retData := global.RDB.LPop(global.RCtx, fmt.Sprintf("list:%s", input.Token)).Val()
|
||||
if retData == "null" {
|
||||
c.JSON(http.StatusOK, gin.H{"result": "数据库没有数据"})
|
||||
return
|
||||
}
|
||||
retData := global.RDB.BLPop(global.RCtx, 0, fmt.Sprintf("list:%s", input.Token)).Val()[1]
|
||||
|
||||
c.String(http.StatusOK, retData)
|
||||
}
|
||||
|
@@ -132,7 +132,6 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token不存在" + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
dedupObject, err := db.GetDedupObject(input.Token)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -145,13 +144,14 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
||||
case "all":
|
||||
keys := global.RDB.Keys(global.RCtx, "dedup:"+input.Token+":*").Val()
|
||||
global.RDB.Del(global.RCtx, keys...)
|
||||
global.RDB.Del(global.RCtx, "delete-list:"+input.Token)
|
||||
default:
|
||||
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()
|
||||
result := global.RDB.LRange(global.RCtx, "delete-list:"+input.Token, int64(-i), -1).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)
|
||||
@@ -162,22 +162,25 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
||||
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, "delete-list:"+input.Token, 0, 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 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "cache_list设置错误 " + err.Error()})
|
||||
return
|
||||
}
|
||||
global.RDB.LTrim(global.RCtx, "list:"+input.Token, 1, int64(-i))
|
||||
|
||||
global.RDB.LTrim(global.RCtx, "list:"+input.Token, 0, int64(-i-1))
|
||||
}
|
||||
//TODO
|
||||
|
||||
//删除去重参考值和原始数据
|
||||
switch input.BothNumber {
|
||||
case "":
|
||||
default:
|
||||
@@ -186,19 +189,20 @@ func DeleteTokenInfoHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "both_number设置错误 " + err.Error()})
|
||||
return
|
||||
}
|
||||
result := global.RDB.LRange(global.RCtx, "delete-list:"+input.Token, 1, int64(i)).Val()
|
||||
|
||||
result := global.RDB.LRange(global.RCtx, "delete-list:"+input.Token, int64(-i), -1).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)
|
||||
}
|
||||
pipe.LTrim(global.RCtx, "delete-list:"+input.Token, 0, int64(-i-1))
|
||||
pipe.LTrim(global.RCtx, "list:"+input.Token, 0, int64(-i-1))
|
||||
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)
|
||||
}
|
||||
|
||||
//输出信息
|
||||
|
Reference in New Issue
Block a user