1
Some checks failed
Build / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
build / Build (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitee (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitlab (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled
Some checks failed
Build / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
build / Build (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitee (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitlab (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled
This commit is contained in:
@ -51,6 +51,14 @@ func NewRedisHelper(addr, password string, db int) *RedisHelper {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedisHelper) GetClient() *redis.Client {
|
||||
return r.client
|
||||
}
|
||||
|
||||
func (r *RedisHelper) GetCtx() context.Context {
|
||||
return r.ctx
|
||||
}
|
||||
|
||||
// 测试连接
|
||||
func (r *RedisHelper) Ping() error {
|
||||
return r.client.Ping(r.ctx).Err()
|
||||
@ -100,6 +108,19 @@ func (r *RedisHelper) SetAdd(key, value string, expireTime time.Duration) error
|
||||
}
|
||||
}
|
||||
|
||||
// 更新zset score
|
||||
func (r *RedisHelper) ZUpdateScore(key string, score float64, value string) error {
|
||||
return r.client.ZAddArgs(r.ctx, key, redis.ZAddArgs{
|
||||
XX: true, // 只更新已存在
|
||||
Members: []redis.Z{
|
||||
{
|
||||
Score: float64(score),
|
||||
Member: value,
|
||||
},
|
||||
},
|
||||
}).Err()
|
||||
}
|
||||
|
||||
// 设置对象
|
||||
func SetObjString[T any](r *RedisHelper, key string, value T) error {
|
||||
keyValue, err := sonic.Marshal(value)
|
||||
@ -736,6 +757,28 @@ func (e *RedisHelper) GetRevRangeScoresSortSet(key string) ([]redis.Z, error) {
|
||||
return e.client.ZRevRangeWithScores(e.ctx, key, 0, -1).Result()
|
||||
}
|
||||
|
||||
// ZSET 中按 score 范围取出成员
|
||||
func (r *RedisHelper) ZRangeByScore(key string, min, max string) ([]string, error) {
|
||||
ctx := context.Background()
|
||||
return r.client.ZRangeByScore(ctx, key, &redis.ZRangeBy{
|
||||
Min: min,
|
||||
Max: max,
|
||||
}).Result()
|
||||
}
|
||||
|
||||
// ZSET 中移除指定成员:
|
||||
func (r *RedisHelper) ZRemValues(key string, members ...string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
// 转换为 interface{} 类型参数
|
||||
vals := make([]interface{}, len(members))
|
||||
for i, m := range members {
|
||||
vals[i] = m
|
||||
}
|
||||
|
||||
return r.client.ZRem(ctx, key, vals...).Err()
|
||||
}
|
||||
|
||||
// 获取最后一条数据
|
||||
func (e *RedisHelper) GetLastSortSet(key string) ([]redis.Z, error) {
|
||||
// 获取最后一个元素及其分数
|
||||
|
||||
Reference in New Issue
Block a user