2026-05-20 16:34:19 +08:00
|
|
|
|
package video
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-10-30 13:25:48 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
const (
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// hotAppliedTTL 覆盖同一次意图的重复投递窗口。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
hotAppliedTTL = 2 * time.Hour
|
2026-10-31 00:03:53 +08:00
|
|
|
|
hotWindowTTL = 2 * time.Hour
|
|
|
|
|
|
// hotOpTimeout 只约束 SetNX / ZINCRBY / EXPIRE 三次往返。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
hotOpTimeout = 100 * time.Millisecond
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// UpdatePopularityCache 给视频的分钟窗口热度加 change。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
//
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// intentKey 非空时先抢 hot:applied:<intentKey> 占位,抢不到说明该意图
|
|
|
|
|
|
// 已生效,直接返回 nil。ZINCRBY 失败必须撤销占位,否则这次增量会被
|
|
|
|
|
|
// 永久跳过。
|
2026-10-30 13:25:48 +08:00
|
|
|
|
func UpdatePopularityCache(ctx context.Context, cache *rediscache.Client, id uint, change int64, intentKey string) error {
|
2026-05-20 16:34:19 +08:00
|
|
|
|
if cache == nil || id == 0 || change == 0 {
|
2026-10-30 13:25:48 +08:00
|
|
|
|
return nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, hotOpTimeout)
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
marker := ""
|
|
|
|
|
|
if intentKey != "" {
|
|
|
|
|
|
marker = cache.Key("hot:applied:%s", intentKey)
|
|
|
|
|
|
claimed, err := cache.SetNX(opCtx, marker, []byte("1"), hotAppliedTTL)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("hot cache: claim intent %q: %w", intentKey, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !claimed {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// 缓存失效是 best-effort,不计入 opCtx 预算。
|
2026-05-20 16:34:19 +08:00
|
|
|
|
_ = cache.Del(context.Background(), cache.Key("video:detail:id=%d", id))
|
2026-05-23 00:36:02 +08:00
|
|
|
|
_ = cache.Del(context.Background(), cache.Key("video:entity:%d", id))
|
2026-05-20 16:34:19 +08:00
|
|
|
|
|
|
|
|
|
|
now := time.Now().UTC().Truncate(time.Minute)
|
|
|
|
|
|
windowKey := cache.Key("hot:video:1m:%s", now.Format("200601021504"))
|
|
|
|
|
|
member := strconv.FormatUint(uint64(id), 10)
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if err := cache.ZincrBy(opCtx, windowKey, member, float64(change)); err != nil {
|
|
|
|
|
|
if marker != "" {
|
|
|
|
|
|
if delErr := cache.Del(context.Background(), marker); delErr != nil {
|
|
|
|
|
|
log.Printf("hot cache: revoke intent marker %s failed: %v", marker, delErr)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return fmt.Errorf("hot cache: zincrby video=%d change=%d: %w", id, change, err)
|
|
|
|
|
|
}
|
2026-05-20 16:34:19 +08:00
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
if err := cache.Expire(opCtx, windowKey, hotWindowTTL); err != nil {
|
|
|
|
|
|
log.Printf("hot cache: expire %s failed (non-fatal): %v", windowKey, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|