点赞与评论的计数只由业务事务写。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 ./... 全绿。
144 lines
4.1 KiB
Go
144 lines
4.1 KiB
Go
package video
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
)
|
||
|
||
type LikeRepository struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewLikeRepository(db *gorm.DB) *LikeRepository {
|
||
return &LikeRepository{db: db}
|
||
}
|
||
|
||
// ApplyLike 插入点赞行,并在同一事务里给 likes_count 与 popularity 各 +1。
|
||
//
|
||
// created=false 表示这一行本来就在(唯一索引 idx_like_video_account 判重),
|
||
// 此时计数与热榜都不能再动:重复投递只能算一次。判重交给数据库,不做
|
||
// "先查再插"——那样并发下必然漏判。
|
||
//
|
||
// 注意:DSN 不能开 clientFoundRows,否则 RowsAffected 恒为 1,判重失效。
|
||
func (r *LikeRepository) ApplyLike(ctx context.Context, like *Like) (created bool, err error) {
|
||
if like == nil || like.VideoID == 0 || like.AccountID == 0 {
|
||
return false, nil
|
||
}
|
||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
res := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(like)
|
||
if res.Error != nil {
|
||
return res.Error
|
||
}
|
||
if res.RowsAffected == 0 {
|
||
return nil
|
||
}
|
||
created = true
|
||
|
||
if err := tx.Model(&Video{}).Where("id = ?", like.VideoID).
|
||
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + 1, 0)")).Error; err != nil {
|
||
return err
|
||
}
|
||
return tx.Model(&Video{}).Where("id = ?", like.VideoID).
|
||
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + 1, 0)")).Error
|
||
})
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
return created, nil
|
||
}
|
||
|
||
// ApplyUnlike 删除点赞行,并在同一事务里把两个计数各 -1。
|
||
//
|
||
// 返回被删的那一行:它的 created_at 是热榜回退时要用的时间。取消点赞必须
|
||
// 把分数减回点赞那一刻的桶,所以这个值必须来自库里,而不是调用方此刻的钟。
|
||
// deleted=false 表示没有这一行(重复投递),此时什么都不改。
|
||
func (r *LikeRepository) ApplyUnlike(ctx context.Context, videoID, accountID uint) (like *Like, deleted bool, err error) {
|
||
if videoID == 0 || accountID == 0 {
|
||
return nil, false, nil
|
||
}
|
||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
var row Like
|
||
if err := tx.Where("video_id = ? AND account_id = ?", videoID, accountID).First(&row).Error; err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
return nil
|
||
}
|
||
return err
|
||
}
|
||
|
||
res := tx.Delete(&Like{}, row.ID)
|
||
if res.Error != nil {
|
||
return res.Error
|
||
}
|
||
if res.RowsAffected == 0 {
|
||
return nil
|
||
}
|
||
deleted = true
|
||
like = &row
|
||
|
||
if err := tx.Model(&Video{}).Where("id = ?", videoID).
|
||
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count - 1, 0)")).Error; err != nil {
|
||
return err
|
||
}
|
||
return tx.Model(&Video{}).Where("id = ?", videoID).
|
||
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity - 1, 0)")).Error
|
||
})
|
||
if err != nil {
|
||
return nil, false, err
|
||
}
|
||
return like, deleted, nil
|
||
}
|
||
|
||
func (r *LikeRepository) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
||
var count int64
|
||
err := r.db.WithContext(ctx).Model(&Like{}).
|
||
Where("video_id = ? AND account_id = ?", videoID, accountID).
|
||
Count(&count).Error
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
return count > 0, nil
|
||
}
|
||
|
||
// 查询给定视频 ID 列表中,哪些视频被指定账户点赞过
|
||
func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, accountID uint) (map[uint]bool, error) {
|
||
likeMap := make(map[uint]bool)
|
||
if len(videoIDs) == 0 {
|
||
return likeMap, nil
|
||
}
|
||
if accountID == 0 {
|
||
return likeMap, nil
|
||
}
|
||
var likes []Like
|
||
err := r.db.WithContext(ctx).Model(&Like{}).
|
||
Where("video_id IN ? AND account_id = ?", videoIDs, accountID).
|
||
Find(&likes).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for _, like := range likes {
|
||
likeMap[like.VideoID] = true
|
||
}
|
||
return likeMap, nil
|
||
}
|
||
|
||
func (r *LikeRepository) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
|
||
var videos []Video
|
||
if accountID == 0 {
|
||
return videos, nil
|
||
}
|
||
err := r.db.WithContext(ctx).
|
||
Model(&Video{}).
|
||
Joins("JOIN likes ON likes.video_id = videos.id").
|
||
Where("likes.account_id = ?", accountID).
|
||
Order("likes.created_at desc").
|
||
Limit(200).
|
||
Find(&videos).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return videos, nil
|
||
}
|