feat: 为用到缓存的部分套上了防击穿锁
This commit is contained in:
@@ -20,27 +20,10 @@ func NewFeedService(repo *FeedRepository, likeRepo *video.LikeRepository, cache
|
|||||||
return &FeedService{repo: repo, likeRepo: likeRepo, cache: cache, cacheTTL: 5 * time.Second}
|
return &FeedService{repo: repo, likeRepo: likeRepo, cache: cache, cacheTTL: 5 * time.Second}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询最新视频
|
||||||
func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore time.Time, viewerAccountID uint) (ListLatestResponse, error) {
|
func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore time.Time, viewerAccountID uint) (ListLatestResponse, error) {
|
||||||
var cacheKey string
|
// 从数据库中查询最新视频
|
||||||
if viewerAccountID == 0 && f.cache != nil {
|
doListLatestFromDB := func() (ListLatestResponse, error) {
|
||||||
before := int64(0)
|
|
||||||
if !latestBefore.IsZero() {
|
|
||||||
before = latestBefore.Unix()
|
|
||||||
}
|
|
||||||
cacheKey = fmt.Sprintf("feed:listLatest:limit=%d:before=%d", limit, before)
|
|
||||||
|
|
||||||
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
b, err := f.cache.GetBytes(cacheCtx, cacheKey)
|
|
||||||
if err == nil {
|
|
||||||
var cached ListLatestResponse
|
|
||||||
if err := json.Unmarshal(b, &cached); err == nil {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
videos, err := f.repo.ListLatest(ctx, limit, latestBefore)
|
videos, err := f.repo.ListLatest(ctx, limit, latestBefore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ListLatestResponse{}, err
|
return ListLatestResponse{}, err
|
||||||
@@ -61,7 +44,66 @@ func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore ti
|
|||||||
NextTime: nextTime,
|
NextTime: nextTime,
|
||||||
HasMore: hasMore,
|
HasMore: hasMore,
|
||||||
}
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
// 先从缓存中查询
|
||||||
|
var cacheKey string
|
||||||
|
if viewerAccountID == 0 && f.cache != nil {
|
||||||
|
before := int64(0)
|
||||||
|
if !latestBefore.IsZero() {
|
||||||
|
before = latestBefore.Unix()
|
||||||
|
}
|
||||||
|
cacheKey = fmt.Sprintf("feed:listLatest:limit=%d:before=%d", limit, before)
|
||||||
|
|
||||||
|
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
b, err := f.cache.GetBytes(cacheCtx, cacheKey)
|
||||||
|
if err == nil {
|
||||||
|
var cached ListLatestResponse
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
} else if rediscache.IsMiss(err) { // 缓存未命中
|
||||||
|
lockKey := "lock:" + cacheKey
|
||||||
|
// 缓存未命中,尝试加锁
|
||||||
|
token, locked, _ := f.cache.Lock(cacheCtx, lockKey, 500*time.Millisecond)
|
||||||
|
if locked {
|
||||||
|
defer func() { _ = f.cache.Unlock(context.Background(), lockKey, token) }()
|
||||||
|
if b, err := f.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached ListLatestResponse
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
} else { // 缓存未命中,从数据库中查询
|
||||||
|
resp, err := doListLatestFromDB()
|
||||||
|
if err != nil {
|
||||||
|
return ListLatestResponse{}, err
|
||||||
|
}
|
||||||
|
if b, err := json.Marshal(resp); err == nil {
|
||||||
|
_ = f.cache.SetBytes(cacheCtx, cacheKey, b, f.cacheTTL)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
} else { // 缓存未命中,其他goroutine正在查询,等待
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
if b, err := f.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached ListLatestResponse
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 缓存中没有查询到结果,从数据库中查询
|
||||||
|
resp, err := doListLatestFromDB()
|
||||||
|
if err != nil {
|
||||||
|
return ListLatestResponse{}, err
|
||||||
|
}
|
||||||
|
// 缓存查询结果
|
||||||
if cacheKey != "" {
|
if cacheKey != "" {
|
||||||
if b, err := json.Marshal(resp); err == nil {
|
if b, err := json.Marshal(resp); err == nil {
|
||||||
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||||
@@ -72,6 +114,7 @@ func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore ti
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按照点赞数查询视频
|
||||||
func (f *FeedService) ListLikesCount(ctx context.Context, limit int, cursor *LikesCountCursor, viewerAccountID uint) (ListLikesCountResponse, error) {
|
func (f *FeedService) ListLikesCount(ctx context.Context, limit int, cursor *LikesCountCursor, viewerAccountID uint) (ListLikesCountResponse, error) {
|
||||||
videos, err := f.repo.ListLikesCountWithCursor(ctx, limit, cursor)
|
videos, err := f.repo.ListLikesCountWithCursor(ctx, limit, cursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -96,26 +139,9 @@ func (f *FeedService) ListLikesCount(ctx context.Context, limit int, cursor *Lik
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按照关注列表查询视频
|
||||||
func (f *FeedService) ListByFollowing(ctx context.Context, limit int, latestBefore time.Time, viewerAccountID uint) (ListByFollowingResponse, error) {
|
func (f *FeedService) ListByFollowing(ctx context.Context, limit int, latestBefore time.Time, viewerAccountID uint) (ListByFollowingResponse, error) {
|
||||||
var cacheKey string
|
doListByFollowingFromDB := func() (ListByFollowingResponse, error) {
|
||||||
if viewerAccountID != 0 && f.cache != nil {
|
|
||||||
before := int64(0)
|
|
||||||
if !latestBefore.IsZero() {
|
|
||||||
before = latestBefore.Unix()
|
|
||||||
}
|
|
||||||
cacheKey = fmt.Sprintf("feed:listByFollowing:limit=%d:accountID=%d:before=%d", limit, viewerAccountID, before)
|
|
||||||
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
b, err := f.cache.GetBytes(cacheCtx, cacheKey)
|
|
||||||
if err == nil {
|
|
||||||
var cached ListByFollowingResponse
|
|
||||||
if err := json.Unmarshal(b, &cached); err == nil {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
videos, err := f.repo.ListByFollowing(ctx, limit, viewerAccountID, latestBefore)
|
videos, err := f.repo.ListByFollowing(ctx, limit, viewerAccountID, latestBefore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ListByFollowingResponse{}, err
|
return ListByFollowingResponse{}, err
|
||||||
@@ -136,6 +162,63 @@ func (f *FeedService) ListByFollowing(ctx context.Context, limit int, latestBefo
|
|||||||
NextTime: nextTime,
|
NextTime: nextTime,
|
||||||
HasMore: hasMore,
|
HasMore: hasMore,
|
||||||
}
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
var cacheKey string
|
||||||
|
if viewerAccountID != 0 && f.cache != nil {
|
||||||
|
before := int64(0)
|
||||||
|
if !latestBefore.IsZero() {
|
||||||
|
before = latestBefore.Unix()
|
||||||
|
}
|
||||||
|
cacheKey = fmt.Sprintf("feed:listByFollowing:limit=%d:accountID=%d:before=%d", limit, viewerAccountID, before)
|
||||||
|
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
b, err := f.cache.GetBytes(cacheCtx, cacheKey)
|
||||||
|
if err == nil {
|
||||||
|
var cached ListByFollowingResponse
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
} else if rediscache.IsMiss(err) { // 缓存未命中
|
||||||
|
lockKey := "lock:" + cacheKey
|
||||||
|
// 缓存未命中,尝试加锁
|
||||||
|
token, locked, _ := f.cache.Lock(cacheCtx, lockKey, 500*time.Millisecond)
|
||||||
|
if locked {
|
||||||
|
defer func() { _ = f.cache.Unlock(context.Background(), lockKey, token) }()
|
||||||
|
if b, err := f.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached ListByFollowingResponse
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
} else { // 缓存未命中,从数据库中查询
|
||||||
|
resp, err := doListByFollowingFromDB()
|
||||||
|
if err != nil {
|
||||||
|
return ListByFollowingResponse{}, err
|
||||||
|
}
|
||||||
|
if b, err := json.Marshal(resp); err == nil {
|
||||||
|
_ = f.cache.SetBytes(cacheCtx, cacheKey, b, f.cacheTTL)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
if b, err := f.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached ListByFollowingResponse
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return cached, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := doListByFollowingFromDB()
|
||||||
|
if err != nil {
|
||||||
|
return ListByFollowingResponse{}, err
|
||||||
|
}
|
||||||
if cacheKey != "" {
|
if cacheKey != "" {
|
||||||
if b, err := json.Marshal(resp); err == nil {
|
if b, err := json.Marshal(resp); err == nil {
|
||||||
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||||
|
|||||||
@@ -69,6 +69,37 @@ func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error)
|
|||||||
if err := json.Unmarshal(b, &cached); err == nil {
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
return &cached, nil
|
return &cached, nil
|
||||||
}
|
}
|
||||||
|
} else if rediscache.IsMiss(err) {
|
||||||
|
lockKey := "lock:" + cacheKey
|
||||||
|
token, locked, _ := vs.cache.Lock(cacheCtx, lockKey, 500*time.Millisecond)
|
||||||
|
if locked {
|
||||||
|
defer func() { _ = vs.cache.Unlock(context.Background(), lockKey, token) }()
|
||||||
|
if b, err := vs.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached Video
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return &cached, nil
|
||||||
|
}
|
||||||
|
} else { // 缓存未命中,从数据库中查询
|
||||||
|
video, err := vs.repo.GetByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if b, err := json.Marshal(video); err == nil {
|
||||||
|
_ = vs.cache.SetBytes(cacheCtx, cacheKey, b, vs.cacheTTL)
|
||||||
|
}
|
||||||
|
return video, nil
|
||||||
|
}
|
||||||
|
} else { // 缓存未命中,其他goroutine正在查询,等待
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
if b, err := vs.cache.GetBytes(cacheCtx, cacheKey); err == nil {
|
||||||
|
var cached Video
|
||||||
|
if err := json.Unmarshal(b, &cached); err == nil {
|
||||||
|
return &cached, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user