窗口右端改为上一个已闭合分钟。当前分钟仍在增长,不能当右端。 一个 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 ./... 全绿。
127 lines
4.1 KiB
Go
127 lines
4.1 KiB
Go
package feed
|
||
|
||
import (
|
||
"context"
|
||
"feedsystem_video_go/internal/social"
|
||
"feedsystem_video_go/internal/video"
|
||
"time"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type FeedRepository struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewFeedRepository(db *gorm.DB) *FeedRepository {
|
||
return &FeedRepository{db: db}
|
||
}
|
||
|
||
// 查询最新视频. limit: 查询数量 latestBefore: 查询早于此时间的视频(零值则不限制)
|
||
func (repo *FeedRepository) ListLatest(ctx context.Context, limit int, latestBefore time.Time) ([]*video.Video, error) {
|
||
var videos []*video.Video
|
||
query := repo.db.WithContext(ctx).Model(&video.Video{}).
|
||
Order("create_time DESC")
|
||
if !latestBefore.IsZero() {
|
||
query = query.Where("create_time < ?", latestBefore)
|
||
}
|
||
if err := query.Limit(limit).Find(&videos).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return videos, nil
|
||
}
|
||
|
||
// 查询点赞数最多的视频. limit: 查询数量 cursor: 游标
|
||
func (repo *FeedRepository) ListLikesCountWithCursor(ctx context.Context, limit int, cursor *LikesCountCursor) ([]*video.Video, error) {
|
||
var videos []*video.Video
|
||
query := repo.db.WithContext(ctx).Model(&video.Video{}).
|
||
Order("likes_count DESC, id DESC")
|
||
|
||
if cursor != nil {
|
||
query = query.Where(
|
||
"(likes_count < ?) OR (likes_count = ? AND id < ?)",
|
||
cursor.LikesCount,
|
||
cursor.LikesCount, cursor.ID,
|
||
)
|
||
}
|
||
|
||
if err := query.Limit(limit).Find(&videos).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return videos, nil
|
||
}
|
||
|
||
// 查询关注用户的视频. limit: 查询数量 viewerAccountID: 查看者账户ID latestBefore: 查询早于此时间的视频(零值则不限制)
|
||
func (repo *FeedRepository) ListByFollowing(ctx context.Context, limit int, viewerAccountID uint, latestBefore time.Time) ([]*video.Video, error) {
|
||
var videos []*video.Video
|
||
query := repo.db.WithContext(ctx).Model(&video.Video{}).
|
||
Order("create_time DESC")
|
||
if viewerAccountID > 0 {
|
||
followingSubQuery := repo.db.WithContext(ctx).
|
||
Model(&social.Social{}).
|
||
Select("vlogger_id").
|
||
Where("follower_id = ?", viewerAccountID)
|
||
query = query.Where("author_id IN (?)", followingSubQuery)
|
||
}
|
||
if !latestBefore.IsZero() {
|
||
query = query.Where("create_time < ?", latestBefore)
|
||
}
|
||
if err := query.Limit(limit).Find(&videos).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return videos, nil
|
||
}
|
||
|
||
type HotWindowRow struct {
|
||
VideoID uint `gorm:"column:video_id"`
|
||
Score int64 `gorm:"column:score"`
|
||
}
|
||
|
||
// ListHotWindow 在 MySQL 上重算热榜窗口,排序定义必须与 Redis 路径一致:
|
||
// 分数降序,同分按视频 ID 降序(ZSET member 补零后就是同一个顺序)。
|
||
//
|
||
// 两个子查询各扫一个 (created_at, video_id) 覆盖索引;UNION ALL 之后再
|
||
// GROUP BY,避免把两张大表 JOIN 起来。
|
||
func (repo *FeedRepository) ListHotWindow(ctx context.Context, from, to time.Time, limit, offset int) ([]HotWindowRow, error) {
|
||
const q = `
|
||
SELECT video_id, CAST(SUM(cnt) AS SIGNED) AS score FROM (
|
||
SELECT video_id, COUNT(*) AS cnt FROM likes
|
||
WHERE created_at >= ? AND created_at < ? GROUP BY video_id
|
||
UNION ALL
|
||
SELECT video_id, COUNT(*) AS cnt FROM comments
|
||
WHERE created_at >= ? AND created_at < ? GROUP BY video_id
|
||
) t
|
||
GROUP BY video_id
|
||
ORDER BY score DESC, video_id DESC
|
||
LIMIT ? OFFSET ?`
|
||
var rows []HotWindowRow
|
||
if err := repo.db.WithContext(ctx).Raw(q, from, to, from, to, limit, offset).Scan(&rows).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return rows, nil
|
||
}
|
||
|
||
func (repo *FeedRepository) GetByIDs(ctx context.Context, ids []uint) ([]*video.Video, error) {
|
||
var videos []*video.Video
|
||
if len(ids) == 0 {
|
||
return videos, nil
|
||
}
|
||
if err := repo.db.WithContext(ctx).Model(&video.Video{}).
|
||
Where("id IN ?", ids).Find(&videos).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return videos, nil
|
||
}
|
||
|
||
func (repo *FeedRepository) ListByTag(ctx context.Context, tagName string, limit int) ([]*video.Video, error) {
|
||
var videos []*video.Video
|
||
err := repo.db.WithContext(ctx).Model(&video.Video{}).Table("videos").
|
||
Joins("JOIN video_tags ON video_tags.video_id = videos.id").
|
||
Joins("JOIN tags ON tags.id = video_tags.tag_id").
|
||
Where("tags.name = ?", tagName).
|
||
Order("videos.create_time desc").
|
||
Limit(limit).
|
||
Find(&videos).Error
|
||
return videos, err
|
||
}
|