窗口右端改为上一个已闭合分钟。当前分钟仍在增长,不能当右端。 一个 as_of 的快照只建一次。ZUNIONSTORE 先写临时键,再用 RENAMENX 原子占位,抢不到的人读赢家。 空并集不再发布成快照。否则这个 as_of 在 TTL 内每次都会命中一张空表,却报告 snapshot_rebuilt=false。 快照 TTL 改为 1 小时。EXPIRE 失败时删掉 dest,避免它继承临时键的 1 分钟寿命而永远翻不到第 2 页。 NextOffset 按 len(members) 推进。视频被删除后 member 仍在,按存活视频数推进会重复读到已跳过的位置。 DB 回退改用 ListHotWindow 重算同一窗口。排序为 score DESC, video_id DESC,与 ZSET 一致。 窗口内无互动时返回 latest。这一支是终结页,NextOffset 为 0。 ZUnionStore 返回并集基数,供调用方判空。新增 RenameNX 包装方法。 删除旧游标字段与 latest_* 配对校验。请求只剩 limit、as_of、offset。 验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
110 lines
3.8 KiB
Go
110 lines
3.8 KiB
Go
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"。
|
||
//
|
||
// 返回并集的基数:调用方要靠它区分"真的空榜"与"并集为空但 dst 已被创建"。
|
||
// ZUNIONSTORE 在没有成员时仍会写出一个空 key,若把它当成有效结果发布,
|
||
// 这个 dst 就会被永久误认为"已经建好的快照"。
|
||
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()
|
||
}
|