Files
VLoop/backend/internal/middleware/redis/zset.go

94 lines
3.0 KiB
Go
Raw Normal View History

2025-12-29 04:10:34 +08:00
package redis
import (
"context"
2026-03-13 15:32:48 +08:00
"errors"
2025-12-29 04:10:34 +08:00
"time"
redis "github.com/redis/go-redis/v9"
)
2026-07-01 19:34:13 +08:00
// ZincrBy 将有序集合 key 中 member 的分数增加 score。
// 如果 member 不存在,则将其添加并设置初始分数为 score。
2025-12-29 04:10:34 +08:00
func (c *Client) ZincrBy(ctx context.Context, key string, member string, score float64) error {
if c == nil || c.rdb == nil {
return nil
}
return c.rdb.ZIncrBy(ctx, key, score, member).Err()
}
2026-07-01 19:34:13 +08:00
// ZAdd 向有序集合 key 中添加一个或多个带分数的成员。
2026-03-13 15:32:48 +08:00
func (c *Client) ZAdd(ctx context.Context, key string, members ...redis.Z) error {
if c == nil || c.rdb == nil {
return nil
}
return c.rdb.ZAdd(ctx, key, members...).Err()
}
2026-07-01 19:34:13 +08:00
// ZRemRangeByRank 移除有序集合 key 中排名在 [start, stop] 范围内的所有成员。
2026-03-13 15:32:48 +08:00
func (c *Client) ZRemRangeByRank(ctx context.Context, key string, start int64, stop int64) error {
if c == nil || c.rdb == nil {
return nil
}
return c.rdb.ZRemRangeByRank(ctx, key, start, stop).Err()
}
2026-07-01 19:34:13 +08:00
// ZRangeWithScores 返回有序集合 key 中排名在指定范围内的成员及其分数。
2026-03-13 15:32:48 +08:00
func (c *Client) ZRangeWithScores(ctx context.Context, key string, start int64, stop int64) ([]redis.Z, error) {
if c == nil || c.rdb == nil {
return nil, errors.New("redis client not initialized")
}
return c.rdb.ZRangeWithScores(ctx, key, start, stop).Result()
}
2026-07-01 19:34:13 +08:00
// Expire 为 key 设置过期时间TTL 过后 key 会被自动删除。
2025-12-29 04:10:34 +08:00
func (c *Client) Expire(ctx context.Context, key string, ttl time.Duration) error {
if c == nil || c.rdb == nil {
return nil
}
return c.rdb.Expire(ctx, key, ttl).Err()
}
2026-07-01 19:34:13 +08:00
// ZUnionStore 计算 keys 指定的多个有序集合的并集,将结果存入 dst。
// aggregate 控制分数聚合方式,可选 "SUM"、"MIN" 或 "MAX"。
2025-12-29 04:10:34 +08:00
func (c *Client) ZUnionStore(ctx context.Context, dst string, keys []string, aggregate string) error {
if c == nil || c.rdb == nil {
return nil
}
return c.rdb.ZUnionStore(ctx, dst, &redis.ZStore{
Keys: keys,
Aggregate: aggregate,
}).Err()
}
2026-07-01 19:34:13 +08:00
// Exists 判断 key 是否存在于 Redis 中。
2025-12-29 04:10:34 +08:00
func (c *Client) Exists(ctx context.Context, key string) (bool, error) {
if c == nil || c.rdb == nil {
return false, nil
}
n, err := c.rdb.Exists(ctx, key).Result()
return n > 0, err
}
2026-07-01 19:34:13 +08:00
// ZRevRange 返回有序集合 key 中排名在 [start, stop] 范围内的成员,按分数从高到低排序。
2025-12-29 04:10:34 +08:00
func (c *Client) ZRevRange(ctx context.Context, key string, start, stop int64) ([]string, error) {
if c == nil || c.rdb == nil {
return nil, nil
}
return c.rdb.ZRevRange(ctx, key, start, stop).Result()
}
2026-07-01 19:34:13 +08:00
// ZRevRangeByScore 返回有序集合 key 中分数在 [min, max] 范围内的成员,按分数从高到低排序。
// offset 和 count 用于分页控制。
2025-12-29 04:10:34 +08:00
func (c *Client) ZRevRangeByScore(ctx context.Context, key string, max, min string, offset, count int64) ([]string, error) {
if c == nil || c.rdb == nil {
return nil, nil
}
return c.rdb.ZRevRangeByScore(ctx, key, &redis.ZRangeBy{
Max: max,
Min: min,
Offset: offset,
Count: count,
}).Result()
}