1、订单增加策略模板

This commit is contained in:
2025-03-31 15:37:29 +08:00
parent 9ca1cd9a19
commit 0b95e32655
6 changed files with 145 additions and 35 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"math"
"reflect"
"strconv"
"time"
@ -609,7 +610,7 @@ func (e *RedisHelper) SignelAdd(key string, score float64, member string) error
_, err := e.client.ZRemRangeByScore(e.ctx, key, scoreStr, scoreStr).Result()
if err != nil {
fmt.Printf("删除score失败", err.Error())
fmt.Printf("删除score失败,err:%s", err.Error())
}
_, err = e.client.ZAdd(e.ctx, key, &redis.Z{
Score: score,
@ -642,6 +643,46 @@ func (e *RedisHelper) DelSortSet(key, member string) error {
return e.client.ZRem(e.ctx, key, member).Err()
}
// RemoveBeforeScore 移除 Sorted Set 中分数小于等于指定值的数据
// key: Sorted Set 的键
// score: 分数上限,所有小于等于此分数的元素将被移除
// 返回值: 移除的元素数量和可能的错误
func (e *RedisHelper) RemoveBeforeScore(key string, score float64) (int64, error) {
if key == "" {
return 0, errors.New("key 不能为空")
}
if math.IsNaN(score) || math.IsInf(score, 0) {
return 0, errors.New("score 必须是有效数字")
}
// 使用 ZRemRangeByScore 移除数据
count, err := e.client.ZRemRangeByScore(e.ctx, key, "-inf", strconv.FormatFloat(score, 'f', -1, 64)).Result()
if err != nil {
return 0, fmt.Errorf("移除 Sorted Set 数据失败, key: %s, score: %f, err: %v", key, score, err)
}
return count, nil
}
// GetNextAfterScore 获取指定分数及之后的第一条数据(包含指定分数)
func (e *RedisHelper) GetNextAfterScore(key string, score float64) (string, error) {
// 使用 ZRangeByScore 获取大于等于 score 的第一条数据
zs, err := e.client.ZRangeByScoreWithScores(e.ctx, key, &redis.ZRangeBy{
Min: fmt.Sprintf("%f", score), // 包含指定分数
Max: "+inf", // 上限为正无穷
Offset: 0, // 从第 0 条开始
Count: 1, // 只取 1 条
}).Result()
if err != nil {
return "", fmt.Errorf("获取数据失败: %v", err)
}
if len(zs) == 0 {
return "", nil // 没有符合条件的元素
}
return zs[0].Member.(string), nil
}
/*
获取sort set 所有数据
*/