点赞与评论的计数只由业务事务写。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 ./... 全绿。
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package video
|
||
|
||
import (
|
||
"context"
|
||
)
|
||
|
||
// Mention 是一条待写入的提及通知。
|
||
type Mention struct {
|
||
RecipientID uint
|
||
SenderID uint
|
||
TargetID uint
|
||
Content string
|
||
DedupeKey string
|
||
}
|
||
|
||
type CommentStore interface {
|
||
// CreateIdempotentWithPopularity 在同一事务内插入评论并给热度加 delta。
|
||
// created=false 表示该意图已落库,调用方不得再加热度。
|
||
CreateIdempotentWithPopularity(ctx context.Context, c *Comment, delta int64) (created bool, err error)
|
||
|
||
GetByID(ctx context.Context, id uint) (*Comment, error)
|
||
|
||
// DeleteWithPopularity 在同一事务内删除评论并向热度 -1。
|
||
// 返回被删的行(拿 created_at 落桶);deleted=false 表示空操作。
|
||
DeleteWithPopularity(ctx context.Context, id uint) (row *Comment, deleted bool, err error)
|
||
|
||
GetAllComments(ctx context.Context, videoID uint) ([]Comment, error)
|
||
|
||
CreateMentions(ctx context.Context, mentions []Mention) error
|
||
FindAccountIDs(ctx context.Context, usernames []string) (map[string]uint, error)
|
||
}
|
||
|
||
// nilIfEmpty 供可空唯一列使用:空串会互撞,NULL 才是"没有这个键"。
|
||
func nilIfEmpty(s string) *string {
|
||
if s == "" {
|
||
return nil
|
||
}
|
||
return &s
|
||
}
|