diff --git a/backend/internal/video/video_repo.go b/backend/internal/video/video_repo.go index fa0046f..c3dd663 100644 --- a/backend/internal/video/video_repo.go +++ b/backend/internal/video/video_repo.go @@ -77,3 +77,21 @@ func (vr *VideoRepository) UpdatePopularity(ctx context.Context, id uint, change } return nil } + +func (vr *VideoRepository) ChangeLikesCount(ctx context.Context, id uint, change int64) error { + if err := vr.db.WithContext(ctx).Model(&Video{}). + Where("id = ?", id). + UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + ?, 0)", change)).Error; err != nil { + return err + } + return nil +} + +func (vr *VideoRepository) ChangePopularity(ctx context.Context, id uint, change int64) error { + if err := vr.db.WithContext(ctx).Model(&Video{}). + Where("id = ?", id). + UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", change)).Error; err != nil { + return err + } + return nil +} diff --git a/backend/internal/video/video_service.go b/backend/internal/video/video_service.go index 4869722..af03ef3 100644 --- a/backend/internal/video/video_service.go +++ b/backend/internal/video/video_service.go @@ -9,17 +9,19 @@ import ( "strings" "time" + "feedsystem_video_go/internal/middleware/rabbitmq" rediscache "feedsystem_video_go/internal/middleware/redis" ) type VideoService struct { - repo *VideoRepository - cache *rediscache.Client - cacheTTL time.Duration + repo *VideoRepository + cache *rediscache.Client + cacheTTL time.Duration + popularityMQ *rabbitmq.PopularityMQ } -func NewVideoService(repo *VideoRepository, cache *rediscache.Client) *VideoService { - return &VideoService{repo: repo, cache: cache, cacheTTL: 5 * time.Minute} +func NewVideoService(repo *VideoRepository, cache *rediscache.Client, popularityMQ *rabbitmq.PopularityMQ) *VideoService { + return &VideoService{repo: repo, cache: cache, cacheTTL: 5 * time.Minute, popularityMQ: popularityMQ} } func (vs *VideoService) Publish(ctx context.Context, video *Video) error { @@ -173,6 +175,12 @@ func (vs *VideoService) UpdatePopularity(ctx context.Context, id uint, change in return err } + if vs.popularityMQ != nil { + if err := vs.popularityMQ.Update(ctx, id, change); err == nil { + return nil + } + } + if vs.cache != nil { // 1) 详情缓存:直接失效(最简单靠谱) _ = vs.cache.Del(context.Background(), fmt.Sprintf("video:detail:id=%d", id))