UpdatePopularityCache 只失效了 video:detail 缓存, 未失效 feed 使用的 video:entity 缓存(1h TTL), 导致推荐列表 likes_count 长时间显示为旧值
30 lines
811 B
Go
30 lines
811 B
Go
package video
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
|
)
|
|
|
|
// 更新视频流行度缓存
|
|
func UpdatePopularityCache(ctx context.Context, cache *rediscache.Client, id uint, change int64) {
|
|
if cache == nil || id == 0 || change == 0 {
|
|
return
|
|
}
|
|
|
|
_ = cache.Del(context.Background(), cache.Key("video:detail:id=%d", id))
|
|
_ = cache.Del(context.Background(), cache.Key("video:entity:%d", id))
|
|
|
|
now := time.Now().UTC().Truncate(time.Minute)
|
|
windowKey := cache.Key("hot:video:1m:%s", now.Format("200601021504"))
|
|
member := strconv.FormatUint(uint64(id), 10)
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
|
defer cancel()
|
|
|
|
_ = cache.ZincrBy(opCtx, windowKey, member, float64(change))
|
|
_ = cache.Expire(opCtx, windowKey, 2*time.Hour)
|
|
}
|