2026-11-01 15:15:15 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// HotWindowMinutes 是热榜窗口覆盖的分钟桶数。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
HotWindowMinutes = 60
|
|
|
|
|
|
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// HotWindowTTL 必须大于读窗口:迟到增量会落在窗口内的旧桶上,
|
|
|
|
|
|
// 桶在滑出窗口之前不能被驱逐。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
HotWindowTTL = 2 * time.Hour
|
|
|
|
|
|
|
|
|
|
|
|
hotOpTimeout = 100 * time.Millisecond
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// HotBucket 返回时刻所属的分钟桶名(UTC)。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
func HotBucket(t time.Time) string {
|
|
|
|
|
|
return t.UTC().Truncate(time.Minute).Format("200601021504")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// HotMember 补零到 12 位:ZSET 同分按字典序返回,补零后字典序等于数值序,
|
|
|
|
|
|
// 才能与 MySQL 的 "ORDER BY score DESC, video_id DESC" 给出同一个顺序。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
func HotMember(videoID uint) string {
|
|
|
|
|
|
return fmt.Sprintf("%012d", videoID)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// ApplyHotDelta 把一次互动写进 occurredAt 所属的分钟桶。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
//
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// 只写派生缓存:失败不回滚任何东西,DB 计数是事实,桶可由 cmd/reconcile 重建。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
func ApplyHotDelta(ctx context.Context, cache *rediscache.Client, videoID uint, delta int64, occurredAt time.Time) error {
|
|
|
|
|
|
if cache == nil || videoID == 0 || delta == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
if occurredAt.IsZero() {
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// 兼容不带 occurred_at 的旧消息。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
occurredAt = now
|
|
|
|
|
|
}
|
|
|
|
|
|
if occurredAt.After(now) {
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// 未来的桶读窗口永远覆盖不到。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
occurredAt = now
|
|
|
|
|
|
}
|
|
|
|
|
|
if now.Sub(occurredAt) > HotWindowTTL {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-01 16:47:31 +08:00
|
|
|
|
// 缓存失效是 best-effort,不占 opCtx 预算。
|
2026-11-01 15:15:15 +08:00
|
|
|
|
_ = cache.Del(context.Background(), cache.Key("video:detail:id=%d", videoID))
|
|
|
|
|
|
_ = cache.Del(context.Background(), cache.Key("video:entity:%d", videoID))
|
|
|
|
|
|
|
|
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, hotOpTimeout)
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
key := cache.Key("hot:video:1m:%s", HotBucket(occurredAt))
|
|
|
|
|
|
if err := cache.ZincrBy(opCtx, key, HotMember(videoID), float64(delta)); err != nil {
|
|
|
|
|
|
return fmt.Errorf("hot window: zincrby video=%d delta=%d key=%s: %w", videoID, delta, key, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := cache.Expire(opCtx, key, HotWindowTTL); err != nil {
|
|
|
|
|
|
log.Printf("hot window: expire %s failed (non-fatal): %v", key, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|