2025-12-07 16:45:00 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2025-12-23 19:04:24 +08:00
|
|
|
|
"encoding/json"
|
2025-12-07 16:45:00 +08:00
|
|
|
|
"errors"
|
2025-12-23 19:04:24 +08:00
|
|
|
|
"fmt"
|
2025-12-26 22:49:17 +08:00
|
|
|
|
"strconv"
|
2025-12-24 20:48:36 +08:00
|
|
|
|
"strings"
|
2025-12-23 19:04:24 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-12-30 01:26:38 +08:00
|
|
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
2025-12-28 18:38:14 +08:00
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
2025-12-07 16:45:00 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type VideoService struct {
|
2025-12-30 01:26:38 +08:00
|
|
|
|
repo *VideoRepository
|
|
|
|
|
|
cache *rediscache.Client
|
|
|
|
|
|
cacheTTL time.Duration
|
|
|
|
|
|
popularityMQ *rabbitmq.PopularityMQ
|
2025-12-07 16:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 01:26:38 +08:00
|
|
|
|
func NewVideoService(repo *VideoRepository, cache *rediscache.Client, popularityMQ *rabbitmq.PopularityMQ) *VideoService {
|
|
|
|
|
|
return &VideoService{repo: repo, cache: cache, cacheTTL: 5 * time.Minute, popularityMQ: popularityMQ}
|
2025-12-07 16:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
2025-12-24 20:48:36 +08:00
|
|
|
|
if video == nil {
|
|
|
|
|
|
return errors.New("video is nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
video.Title = strings.TrimSpace(video.Title)
|
|
|
|
|
|
video.PlayURL = strings.TrimSpace(video.PlayURL)
|
|
|
|
|
|
video.CoverURL = strings.TrimSpace(video.CoverURL)
|
|
|
|
|
|
|
2025-12-07 16:45:00 +08:00
|
|
|
|
if video.Title == "" {
|
|
|
|
|
|
return errors.New("title is required")
|
|
|
|
|
|
}
|
|
|
|
|
|
if video.PlayURL == "" {
|
|
|
|
|
|
return errors.New("play url is required")
|
|
|
|
|
|
}
|
2025-12-09 21:11:29 +08:00
|
|
|
|
if video.CoverURL == "" {
|
|
|
|
|
|
return errors.New("cover url is required")
|
|
|
|
|
|
}
|
2025-12-07 16:45:00 +08:00
|
|
|
|
if err := vs.repo.CreateVideo(ctx, video); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 18:51:17 +08:00
|
|
|
|
func (vs *VideoService) Delete(ctx context.Context, id uint, authorID uint) error {
|
|
|
|
|
|
video, err := vs.repo.GetByID(ctx, id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-24 20:48:36 +08:00
|
|
|
|
if video == nil {
|
|
|
|
|
|
return errors.New("video not found")
|
|
|
|
|
|
}
|
2025-12-16 18:51:17 +08:00
|
|
|
|
if video.AuthorID != authorID {
|
|
|
|
|
|
return errors.New("unauthorized")
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := vs.repo.DeleteVideo(ctx, id); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-24 20:48:36 +08:00
|
|
|
|
if vs.cache != nil {
|
|
|
|
|
|
cacheKey := fmt.Sprintf("video:detail:id=%d", id)
|
|
|
|
|
|
_ = vs.cache.Del(context.Background(), cacheKey)
|
|
|
|
|
|
}
|
2025-12-16 18:51:17 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-07 16:45:00 +08:00
|
|
|
|
func (vs *VideoService) ListByAuthorID(ctx context.Context, authorID uint) ([]Video, error) {
|
|
|
|
|
|
videos, err := vs.repo.ListByAuthorID(ctx, int64(authorID))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return videos, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error) {
|
2025-12-24 20:48:36 +08:00
|
|
|
|
cacheKey := fmt.Sprintf("video:detail:id=%d", id)
|
|
|
|
|
|
|
|
|
|
|
|
getCached := func() (*Video, bool) {
|
|
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
2025-12-23 19:04:24 +08:00
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
2025-12-24 20:48:36 +08:00
|
|
|
|
b, err := vs.cache.GetBytes(opCtx, cacheKey)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, false
|
|
|
|
|
|
}
|
|
|
|
|
|
var cached Video
|
|
|
|
|
|
if err := json.Unmarshal(b, &cached); err != nil {
|
|
|
|
|
|
return nil, false
|
|
|
|
|
|
}
|
|
|
|
|
|
return &cached, true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setCached := func(video *Video) {
|
|
|
|
|
|
b, err := json.Marshal(video)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
_ = vs.cache.SetBytes(opCtx, cacheKey, b, vs.cacheTTL)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if vs.cache != nil {
|
|
|
|
|
|
if v, ok := getCached(); ok {
|
|
|
|
|
|
return v, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
|
|
|
|
b, err := vs.cache.GetBytes(opCtx, cacheKey)
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
if err == nil {
|
2025-12-23 19:04:24 +08:00
|
|
|
|
var cached Video
|
|
|
|
|
|
if err := json.Unmarshal(b, &cached); err == nil {
|
|
|
|
|
|
return &cached, nil
|
|
|
|
|
|
}
|
2025-12-24 14:09:10 +08:00
|
|
|
|
} else if rediscache.IsMiss(err) {
|
|
|
|
|
|
lockKey := "lock:" + cacheKey
|
2025-12-24 20:48:36 +08:00
|
|
|
|
|
|
|
|
|
|
lockCtx, lockCancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
|
|
|
|
token, locked, lockErr := vs.cache.Lock(lockCtx, lockKey, 2*time.Second)
|
|
|
|
|
|
lockCancel()
|
|
|
|
|
|
|
|
|
|
|
|
if lockErr == nil && locked {
|
2025-12-24 14:09:10 +08:00
|
|
|
|
defer func() { _ = vs.cache.Unlock(context.Background(), lockKey, token) }()
|
2025-12-24 20:48:36 +08:00
|
|
|
|
|
|
|
|
|
|
if v, ok := getCached(); ok {
|
|
|
|
|
|
return v, nil
|
2025-12-24 14:09:10 +08:00
|
|
|
|
}
|
2025-12-24 20:48:36 +08:00
|
|
|
|
|
|
|
|
|
|
video, err := vs.repo.GetByID(ctx, id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
setCached(video)
|
|
|
|
|
|
return video, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 没拿到锁:等待别人回填缓存
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return nil, ctx.Err()
|
|
|
|
|
|
case <-time.After(20 * time.Millisecond):
|
|
|
|
|
|
}
|
|
|
|
|
|
if v, ok := getCached(); ok {
|
|
|
|
|
|
return v, nil
|
2025-12-24 14:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-23 19:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-07 16:45:00 +08:00
|
|
|
|
video, err := vs.repo.GetByID(ctx, id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-12-23 19:04:24 +08:00
|
|
|
|
if vs.cache != nil {
|
2025-12-24 20:48:36 +08:00
|
|
|
|
setCached(video)
|
2025-12-23 19:04:24 +08:00
|
|
|
|
}
|
2025-12-07 16:45:00 +08:00
|
|
|
|
return video, nil
|
|
|
|
|
|
}
|
2025-12-09 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
|
func (vs *VideoService) UpdateLikesCount(ctx context.Context, id uint, likesCount int64) error {
|
|
|
|
|
|
if err := vs.repo.UpdateLikesCount(ctx, id, likesCount); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-12-26 22:49:17 +08:00
|
|
|
|
|
|
|
|
|
|
func (vs *VideoService) UpdatePopularity(ctx context.Context, id uint, change int64) error {
|
|
|
|
|
|
if err := vs.repo.UpdatePopularity(ctx, id, change); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 01:26:38 +08:00
|
|
|
|
if vs.popularityMQ != nil {
|
|
|
|
|
|
if err := vs.popularityMQ.Update(ctx, id, change); err == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 22:49:17 +08:00
|
|
|
|
if vs.cache != nil {
|
|
|
|
|
|
// 1) 详情缓存:直接失效(最简单靠谱)
|
|
|
|
|
|
_ = vs.cache.Del(context.Background(), fmt.Sprintf("video:detail:id=%d", id))
|
|
|
|
|
|
|
|
|
|
|
|
// 2) 热榜:写到“时间窗ZSET”,不要用 detail key
|
|
|
|
|
|
now := time.Now().UTC().Truncate(time.Minute)
|
|
|
|
|
|
windowKey := "hot:video:1m:" + now.Format("200601021504")
|
|
|
|
|
|
member := strconv.FormatUint(uint64(id), 10)
|
|
|
|
|
|
|
|
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
_ = vs.cache.ZincrBy(opCtx, windowKey, member, float64(change))
|
|
|
|
|
|
_ = vs.cache.Expire(opCtx, windowKey, 2*time.Hour)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|