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

82 lines
2.1 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"
)
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-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()
}
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()
}
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()
}
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()
}
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()
}
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
}
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()
}
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()
}