Files
VLoop/backend/internal/middleware/redis/zset.go
huanghaosheng d782ccbb53 refactor(p0-5): 收敛热度相关注释
背景叙述与历史取舍已写进 `docs/design/hot-ranking.md`,从源码删除。
只保留不变式与安全约束:`clientFoundRows` 禁令、唯一索引判重、
成员补零的排序约定、快照用 `RENAMENX` 只建一次、`EXPIRE` 失败即删键。

`video_service.go` 的 `NewVideoService` 注释原本复述已删方法的来历,一并收敛。
`zset.go` 的 `ZUnionStore` 与 `RenameNX` 注释各压到四行以内。

验证:go build ./... 通过。
验证:go vet ./... 通过。
验证:go test -count=1 ./... 全绿(本机无 cgo,未跑 -race)。
验证:go test -tags integration -p 1 -count=1 ./internal/feed/ ./internal/video/ ./internal/worker/ 全绿。
2026-11-01 16:47:31 +08:00

107 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package redis
import (
"context"
"errors"
"time"
redis "github.com/redis/go-redis/v9"
)
// ZincrBy 将有序集合 key 中 member 的分数增加 score。
// 如果 member 不存在,则将其添加并设置初始分数为 score。
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()
}
// ZAdd 向有序集合 key 中添加一个或多个带分数的成员。
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()
}
// ZRemRangeByRank 移除有序集合 key 中排名在 [start, stop] 范围内的所有成员。
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()
}
// ZRangeWithScores 返回有序集合 key 中排名在指定范围内的成员及其分数。
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()
}
// Expire 为 key 设置过期时间,TTL 过后 key 会被自动删除。
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()
}
// ZUnionStore 计算 keys 的并集存入 dst,aggregate 可选 "SUM"/"MIN"/"MAX"。
//
// 返回基数:ZUNIONSTORE 在无成员时仍会写出空 key,调用方要靠它区分
// "空结果"与"已写入的空集合"。
func (c *Client) ZUnionStore(ctx context.Context, dst string, keys []string, aggregate string) (int64, error) {
if c == nil || c.rdb == nil {
return 0, nil
}
return c.rdb.ZUnionStore(ctx, dst, &redis.ZStore{
Keys: keys,
Aggregate: aggregate,
}).Result()
}
// RenameNX 仅在 dst 不存在时把 src 改名为 dst。
//
// 它是"快照只建一次"的关键:EXISTS + ZUNIONSTORE 是 check-then-act,
// 并发请求会各自判定"不存在"并互相覆盖;RENAMENX 把占位与发布合成一次原子操作。
func (c *Client) RenameNX(ctx context.Context, src, dst string) (bool, error) {
if c == nil || c.rdb == nil {
return false, errors.New("redis client not initialized")
}
return c.rdb.RenameNX(ctx, src, dst).Result()
}
// Exists 判断 key 是否存在于 Redis 中。
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
}
// ZRevRange 返回有序集合 key 中排名在 [start, stop] 范围内的成员,按分数从高到低排序。
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()
}
// ZRevRangeByScore 返回有序集合 key 中分数在 [min, max] 范围内的成员,按分数从高到低排序。
// offset 和 count 用于分页控制。
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()
}