窗口右端改为上一个已闭合分钟。当前分钟仍在增长,不能当右端。 一个 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 ./... 全绿。
89 lines
3.3 KiB
Go
89 lines
3.3 KiB
Go
package feed
|
||
|
||
import "time"
|
||
|
||
type FeedAuthor struct {
|
||
ID uint `json:"id"`
|
||
Username string `json:"username"`
|
||
}
|
||
|
||
type FeedVideoItem struct {
|
||
ID uint `json:"id"`
|
||
Author FeedAuthor `json:"author"`
|
||
Title string `json:"title"`
|
||
Description string `json:"description,omitempty"`
|
||
PlayURL string `json:"play_url"`
|
||
CoverURL string `json:"cover_url"`
|
||
CreateTime int64 `json:"create_time"`
|
||
LikesCount int64 `json:"likes_count"`
|
||
IsLiked bool `json:"is_liked"`
|
||
}
|
||
|
||
type ListLatestRequest struct {
|
||
Limit int `json:"limit"`
|
||
LatestTime int64 `json:"latest_time"`
|
||
}
|
||
|
||
type ListLatestResponse struct {
|
||
VideoList []FeedVideoItem `json:"video_list"`
|
||
NextTime int64 `json:"next_time"`
|
||
HasMore bool `json:"has_more"`
|
||
}
|
||
|
||
type ListLikesCountRequest struct {
|
||
Limit int `json:"limit"`
|
||
LikesCountBefore *int64 `json:"likes_count_before,omitempty"`
|
||
IDBefore *uint `json:"id_before,omitempty"`
|
||
}
|
||
|
||
type LikesCountCursor struct {
|
||
LikesCount int64
|
||
ID uint
|
||
}
|
||
|
||
type ListLikesCountResponse struct {
|
||
VideoList []FeedVideoItem `json:"video_list"`
|
||
NextLikesCountBefore *int64 `json:"next_likes_count_before,omitempty"`
|
||
NextIDBefore *uint `json:"next_id_before,omitempty"`
|
||
HasMore bool `json:"has_more"`
|
||
}
|
||
|
||
type ListByFollowingRequest struct {
|
||
Limit int `json:"limit"`
|
||
LatestTime int64 `json:"latest_time"`
|
||
}
|
||
|
||
type ListByFollowingResponse struct {
|
||
VideoList []FeedVideoItem `json:"video_list"`
|
||
NextTime int64 `json:"next_time"`
|
||
HasMore bool `json:"has_more"`
|
||
}
|
||
|
||
type ListByPopularityRequest struct {
|
||
Limit int `json:"limit"`
|
||
AsOf int64 `json:"as_of"` // 0 表示"上一个闭合分钟";只接受更早且不超过 hotSnapshotTTL 的值
|
||
Offset int `json:"offset"` // 下一页从这里开始;第一页传 0
|
||
}
|
||
|
||
type ListByPopularityResponse struct {
|
||
VideoList []FeedVideoItem `json:"video_list"`
|
||
AsOf int64 `json:"as_of"` // 本次快照的标识(分钟右端,必为已闭合分钟)
|
||
NextOffset int `json:"next_offset"`
|
||
HasMore bool `json:"has_more"`
|
||
// RankingSource 让客户端知道这一页来自哪里。它同时是一个**重置信号**:
|
||
// "hot" Redis 点时快照(正常)
|
||
// "hot_db" MySQL 重算同一窗口(Redis 缺席;没有快照,翻页只是近似稳定)
|
||
// "latest" 窗口内无互动,返回最新视频(终结页,HasMore 恒为 false)
|
||
// 与上一页不同,说明榜换了,offset 不再有意义,客户端应当从 0 重来。
|
||
RankingSource string `json:"ranking_source"`
|
||
// SnapshotRebuilt 为 true 表示服务端刚重建过快照(原来的过期或被驱逐),
|
||
// 这一页可能与上一页重叠:客户端应当从 offset 0 重来。
|
||
SnapshotRebuilt bool `json:"snapshot_rebuilt"`
|
||
|
||
// Deprecated: 旧版用 DB 的 (popularity, create_time, id) 游标继续翻页。
|
||
// 那套游标与 ZSET 的排名无关(N7),服务端已不再读取,字段仅为兼容保留。
|
||
NextLatestPopularity *int64 `json:"next_latest_popularity,omitempty"`
|
||
NextLatestBefore *time.Time `json:"next_latest_before,omitempty"`
|
||
NextLatestIDBefore *uint `json:"next_latest_id_before,omitempty"`
|
||
}
|