01純 TTL 過期
讀路徑只塞 Redis 加 60 秒到期,寫路徑完全不管快取。
func GetProduct(ctx ctx.Context, id int64) (*Product, error) { key := fmt.Sprintf("product:%d", id) if raw, err := rdb.Get(ctx, key).Bytes(); err == nil { return decode(raw) } p, err := db.LoadProduct(ctx, id) if err != nil { return nil, err } rdb.Set(ctx, key, encode(p), 60*time.Second) return p, nil } // 寫路徑不動快取,等 TTL 自然過期 func UpdatePrice(ctx ctx.Context, id int64, price int64) error { return db.SetPrice(ctx, id, price) }
優點
缺點
實作最簡單,三十行內搞定
改價後最久要等 60 秒才生效,營運會抱怨
讀寫路徑解耦,寫端不會因 Redis 掛掉失敗
熱門商品快取過期瞬間會出現 cache stampede
Redis 異常時退化成直打 DB,行為可預測
記憶體用量不可控,冷門商品也佔位 60 秒
DB 命中率: ~6%
資料新鮮度: ≤60s
實作複雜度: 低
熱點防護: 無