refactor(video): 计数改由业务事务维护并删除热度第二条写路径

点赞与评论的计数只由业务事务写。ApplyLike、ApplyUnlike、CreateIdempotentWithPopularity 与 DeleteWithPopularity 各自在单事务内完成插行与计数。
热榜增量改为提交后的 best-effort 写。ApplyHotDelta 按行的 created_at 落分钟桶,写失败只记日志,DB 计数仍然正确。
删除 PopularityMQ 与 PopularityWorker。同一次互动因此只有一个写者,不再有两个门闩各自放行。
删除 hot:applied 占位键。判重改由 likes 的唯一索引与 uk_comments_author_key 负责。
DeleteWithPopularity 先读行再删行。评论删除因此回退热度,重复投递不再多减一次。
likes 与 comments 各加 (created_at, video_id) 覆盖索引,服务热榜窗口查询。
验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
This commit is contained in:
2026-11-01 15:15:15 +08:00
parent c0210b61ba
commit 1b47c9f027
26 changed files with 924 additions and 626 deletions

View File

@@ -19,10 +19,10 @@ type VideoService struct {
cacheTTL time.Duration
}
// NewVideoService 不再接收 popularityMQ:热度写入只有两个入口——
// PopularityWorker(异步)与 LikeService/CommentService 的兜底路径,
// 视频服务从来不直接改热度。原来这里挂着一个没人调用的 UpdatePopularity,
// 它是热度写入的第三份实现,且漏删 video:entity 缓存(P0-4 的 N3)。
// NewVideoService 不涉及热度写入:likes_count / popularity 只由点赞与评论的
// 业务事务维护(LikeRepository.ApplyLike、CommentRepository.CreateIdempotentWithPopularity、
// DeleteWithPopularity),视频服务从来不直接改热度。原来这里挂着一个没人调用的
// UpdatePopularity,它是热度写入的第三份实现,且漏删 video:entity 缓存(P0-4 的 N3)。
func NewVideoService(repo *VideoRepository, cache *rediscache.Client) *VideoService {
return &VideoService{repo: repo, cache: cache, cacheTTL: 5 * time.Minute}
}