feat:为video的getdetail添加redis缓存
This commit is contained in:
@@ -34,7 +34,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
|
|||||||
}
|
}
|
||||||
// video
|
// video
|
||||||
videoRepository := video.NewVideoRepository(db)
|
videoRepository := video.NewVideoRepository(db)
|
||||||
videoService := video.NewVideoService(videoRepository)
|
videoService := video.NewVideoService(videoRepository, cache)
|
||||||
videoHandler := video.NewVideoHandler(videoService, accountService)
|
videoHandler := video.NewVideoHandler(videoService, accountService)
|
||||||
videoGroup := r.Group("/video")
|
videoGroup := r.Group("/video")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,15 +2,22 @@ package video
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
rediscache "feedsystem_video_go/internal/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VideoService struct {
|
type VideoService struct {
|
||||||
repo *VideoRepository
|
repo *VideoRepository
|
||||||
|
cache *rediscache.Client
|
||||||
|
cacheTTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVideoService(repo *VideoRepository) *VideoService {
|
func NewVideoService(repo *VideoRepository, cache *rediscache.Client) *VideoService {
|
||||||
return &VideoService{repo: repo}
|
return &VideoService{repo: repo, cache: cache, cacheTTL: 5 * time.Minute}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
||||||
@@ -52,10 +59,32 @@ func (vs *VideoService) ListByAuthorID(ctx context.Context, authorID uint) ([]Vi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error) {
|
func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error) {
|
||||||
|
if vs.cache != nil {
|
||||||
|
cacheKey := fmt.Sprintf("video:detail:id=%d", id)
|
||||||
|
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if b, err := vs.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached Video
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return &cached, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
video, err := vs.repo.GetByID(ctx, id)
|
video, err := vs.repo.GetByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if vs.cache != nil {
|
||||||
|
cacheKey := fmt.Sprintf("video:detail:id=%d", id)
|
||||||
|
if b, err := json.Marshal(video); err == nil {
|
||||||
|
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
_ = vs.cache.SetBytes(cacheCtx, cacheKey, b, vs.cacheTTL)
|
||||||
|
}
|
||||||
|
}
|
||||||
return video, nil
|
return video, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user