feat:为ListByFollowing方法添加上redis缓存

This commit is contained in:
Leon
2025-12-24 13:04:38 +08:00
parent f0411b4577
commit 94140c3c13
4 changed files with 41 additions and 7 deletions

View File

@@ -96,8 +96,27 @@ func (f *FeedService) ListLikesCount(ctx context.Context, limit int, cursor *Lik
return resp, nil
}
func (f *FeedService) ListByFollowing(ctx context.Context, limit int, viewerAccountID uint) (ListByFollowingResponse, error) {
videos, err := f.repo.ListByFollowing(ctx, limit, viewerAccountID)
func (f *FeedService) ListByFollowing(ctx context.Context, limit int, latestBefore time.Time, viewerAccountID uint) (ListByFollowingResponse, error) {
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
}
}
}
videos, err := f.repo.ListByFollowing(ctx, limit, viewerAccountID, latestBefore)
if err != nil {
return ListByFollowingResponse{}, err
}
@@ -117,6 +136,13 @@ func (f *FeedService) ListByFollowing(ctx context.Context, limit int, viewerAcco
NextTime: nextTime,
HasMore: hasMore,
}
if cacheKey != "" {
if b, err := json.Marshal(resp); err == nil {
cacheCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
defer cancel()
_ = f.cache.SetBytes(cacheCtx, cacheKey, b, f.cacheTTL)
}
}
return resp, nil
}