feat(feed): 热榜读路径收口到闭合分钟的只建一次快照
窗口右端改为上一个已闭合分钟。当前分钟仍在增长,不能当右端。 一个 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 ./... 全绿。
This commit is contained in:
@@ -51,14 +51,30 @@ func (c *Client) Expire(ctx context.Context, key string, ttl time.Duration) erro
|
||||
|
||||
// ZUnionStore 计算 keys 指定的多个有序集合的并集,将结果存入 dst。
|
||||
// aggregate 控制分数聚合方式,可选 "SUM"、"MIN" 或 "MAX"。
|
||||
func (c *Client) ZUnionStore(ctx context.Context, dst string, keys []string, aggregate string) error {
|
||||
//
|
||||
// 返回并集的基数:调用方要靠它区分"真的空榜"与"并集为空但 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 nil
|
||||
return 0, nil
|
||||
}
|
||||
return c.rdb.ZUnionStore(ctx, dst, &redis.ZStore{
|
||||
Keys: keys,
|
||||
Aggregate: aggregate,
|
||||
}).Err()
|
||||
}).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 中。
|
||||
|
||||
Reference in New Issue
Block a user