优化追加次数

This commit is contained in:
Yun
2024-05-28 15:02:57 +08:00
parent b98a421116
commit bdd0ee714b
2 changed files with 22 additions and 7 deletions
+18 -3
View File
@@ -263,12 +263,21 @@ func (c *Cluster) getNextTime() {
// fmt.Println(val.ExtendData, val.JobData, nextTime)
// 内部判定是否重复
cacheKey := fmt.Sprintf("%s_%s_%d", c.keyPrefix, val.TaskId, nextTime.Unix())
_, err := c.cache.Get(cacheKey)
cacheKey := fmt.Sprintf("%s_%s_%d", c.keyPrefix, val.TaskId, nextTime.UnixMilli())
cacheVal, err := c.cache.Get(cacheKey)
if err == nil {
// 缓存已有值
return true
}
valueNum := int(0)
if cacheVal != nil {
valueNum = cacheVal.(int)
}
if valueNum > 2 {
// 重试2次还是失败就不执行了
return true
}
// fmt.Println("计算时间1", val.ExtendData, time.UnixMilli(nextTime.UnixMilli()).Format("2006-01-02 15:04:05"))
// redis lua脚本,尝试设置nx锁时间为一分钟,如果能设置进去则添加到有序集合zsetKey
script := `
@@ -294,11 +303,17 @@ func (c *Cluster) getNextTime() {
res, err := c.redis.Eval(c.ctx, script, []string{c.zsetKey}, cacheKey, expireTime.Seconds(), nextTime.UnixMilli(), val.TaskId).Result()
valueNum++
if err == nil && res.(string) == "SUCCESS" {
// 设置成功
c.cache.Set(cacheKey, "", expireTime)
valueNum = 10
// fmt.Println("计算时间2", val.ExtendData, time.UnixMilli(nextTime.UnixMilli()).Format("2006-01-02 15:04:05"))
}
c.cache.Set(cacheKey, valueNum, expireTime)
return true
})