增加商品多规格探测缓存

This commit is contained in:
Christ committed 2026-08-15 16:08:37 +08:00
1 parent 8dbe2d6616
commit 6bc29829ba
4 files changed
+106 -1

No files matched your search

@@ -743,4 +743,5 @@ npm --prefix frontend run build
| 2026-08-15 | P2 订单详情分片事务写入切片 | 已完成 | 订单详情远端请求仍在锁外执行;每个详情分片先收集成功结果,再通过订单应用 `UnitOfWork` 在单事务内提交,避免逐订单独立事务和分片内部分提交;事务失败时整片结果统一失败并保留远端会话收束逻辑;订单刷新定向测试、`make check`、架构和中文注释门禁通过。尚未实现真正单 SQL 多行 UPSERT、游标分页和后台 Job 化,P2 批量同步仍保持“进行中” |
| 2026-08-15 | P2 订单刷新复合游标扫描切片 | 已完成 | 订单刷新应用 Port 移除 OFFSET 分页入口,数据库新增 `created_at + order_id` 复合游标查询并统一扫描模型;刷新流程按游标读取每个账号订单,加入游标不推进保护;1001 条订单无重复扫描、订单/Server 定向测试和中文注释门禁通过。用户订单列表 HTTP 分页仍是页码语义;真正单 SQL 多行 UPSERT、后台 Job 化、SQLite/MySQL/Postgres 查询计划证据仍待完成,P2 批量同步保持“进行中” |
| 2026-08-15 | P2 商品多规格探测批量与并发边界切片 | 已完成 | 商品同步先用 `MultiSpecFlags` 一次读取本地多规格标记,消除逐商品数据库查询;剩余远端详情探测使用 4 路有界并发,Session 过期可取消同批次探测,普通单项失败保留告警并继续;新增批量标记和并发上限回归测试,定向测试与中文注释门禁通过。跨请求缓存、跨批次去重、平台限流指标和 MySQL/Postgres 查询计划仍待完成,P2 批量同步保持“进行中” |
| 2026-08-15 | P2 商品多规格跨请求缓存切片 | 已完成 | Server 新增按账号/商品隔离的 10 分钟 TTL 多规格缓存;同步成功结果(含 false)均缓存,后续同步命中缓存时跳过远端探测,过期项自动清理;新增缓存命中、过期和并发探测回归测试,注释门禁通过。跨实例共享缓存、主动失效通知、平台限流指标和 MySQL/Postgres 查询计划仍待完成,P2 批量同步保持“进行中” |
| 2026-08-15 | P2 订单复合游标索引与查询计划切片 | 已完成 | SQLite/MySQL/Postgres 新增 `idx_orders_cursor(cookie_id, deleted_at, created_at, order_id)` 复合索引;新增 SQLite `EXPLAIN QUERY PLAN` 回归,确认订单游标查询使用该索引;迁移、订单扫描和中文注释门禁通过。MySQL/Postgres 实际执行计划仍需外部数据库环境验证,P2 批量同步保持“进行中” |
+66 -1
View File
@@ -763,6 +763,64 @@ func (s *Server) syncSyncedItems(ctx context.Context, cookieID string, items []m
// itemSpecProbeConcurrency 限制商品多规格远端探测的并发数,避免同步洪峰压垮平台接口。
const itemSpecProbeConcurrency = 4
// itemSpecCacheTTL 定义商品多规格探测结果的短期缓存时长。
const itemSpecCacheTTL = 10 * time.Minute
// itemSpecCacheEntry 保存商品多规格结果及其过期时间。
type itemSpecCacheEntry struct {
// isMultiSpec 表示商品是否包含多规格。
isMultiSpec bool
// expiresAt 表示该缓存项失效的时间。
expiresAt time.Time
}
// itemSpecCacheKey 生成账号与商品组合的缓存键,避免不同账号同商品 ID 相互污染。
func itemSpecCacheKey(cookieID, itemID string) string {
return cookieID + "\x00" + itemID
}
// cachedItemSpec 读取未过期的商品多规格缓存。
func (s *Server) cachedItemSpec(cookieID, itemID string) (bool, bool) {
if s == nil {
return false, false
}
s.itemSpecCacheMu.Lock()
defer s.itemSpecCacheMu.Unlock()
// key 表示账号与商品组合的缓存键。
key := itemSpecCacheKey(cookieID, itemID)
// entry、exists 保存缓存项及其存在状态。
entry, exists := s.itemSpecCache[key]
if !exists {
return false, false
}
if time.Now().After(entry.expiresAt) {
delete(s.itemSpecCache, key)
return false, false
}
return entry.isMultiSpec, true
}
// cacheItemSpec 写入商品多规格结果并清理已过期缓存项。
func (s *Server) cacheItemSpec(cookieID, itemID string, isMultiSpec bool) {
if s == nil || itemID == "" {
return
}
s.itemSpecCacheMu.Lock()
defer s.itemSpecCacheMu.Unlock()
if s.itemSpecCache == nil {
s.itemSpecCache = make(map[string]itemSpecCacheEntry)
}
// now 表示本次写入缓存的时间基准。
now := time.Now()
// key、entry 分别表示缓存键及其缓存项。
for key, entry := range s.itemSpecCache {
if now.After(entry.expiresAt) {
delete(s.itemSpecCache, key)
}
}
s.itemSpecCache[itemSpecCacheKey(cookieID, itemID)] = itemSpecCacheEntry{isMultiSpec: isMultiSpec, expiresAt: now.Add(itemSpecCacheTTL)}
}
// enrichSyncedItemMultiSpec 批量读取本地标记并受限并发探测剩余商品的多规格状态。
func (s *Server) enrichSyncedItemMultiSpec(ctx context.Context, client mtop.Client, cookies, cookieID string, items []mtop.ItemListItem) error {
// fetcher、ok 保存fetcher、ok,供当前处理流程使用
@@ -784,10 +842,16 @@ func (s *Server) enrichSyncedItemMultiSpec(ctx context.Context, client mtop.Clie
// candidates 保存需要访问远端接口的商品下标。
candidates := make([]int, 0, len(items))
// index 表示当前商品在同步列表中的下标。
// index 表示当前商品在同步列表中的下标。
for index := range items {
if items[index].IsMultiSpec || localFlags[items[index].ID] {
items[index].IsMultiSpec = true
s.cacheItemSpec(cookieID, items[index].ID, true)
continue
}
// cachedValue、cached 表示当前商品是否命中短期探测缓存。
cachedValue, cached := s.cachedItemSpec(cookieID, items[index].ID)
if cached {
items[index].IsMultiSpec = cachedValue
continue
}
candidates = append(candidates, index)
@@ -837,6 +901,7 @@ func (s *Server) enrichSyncedItemMultiSpec(ctx context.Context, client mtop.Clie
}
return
}
s.cacheItemSpec(cookieID, items[index].ID, isMultiSpec)
items[index].IsMultiSpec = isMultiSpec
}(index)
}
+34
View File
@@ -641,12 +641,15 @@ func TestEnrichSyncedItemMultiSpecLimitsProbeConcurrency(t *testing.T) {
var stateMu sync.Mutex
// active、maxActive 保存当前及观测到的最大远端探测并发数。
active, maxActive := 0, 0
// probeCalls 统计测试客户端收到的商品详情探测请求数。
probeCalls := 0
// client 是带并发计数的商品详情测试客户端。
client := withMTopTransport(roundTripFunc(func(req *http.Request) (*http.Response, error) {
if !strings.Contains(req.URL.String(), "mtop.taobao.idle.pc.detail") {
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{}`)), Request: req}, nil
}
stateMu.Lock()
probeCalls++
active++
if active > maxActive {
maxActive = active
@@ -678,6 +681,19 @@ func TestEnrichSyncedItemMultiSpecLimitsProbeConcurrency(t *testing.T) {
t.Fatalf("item %d was not marked multi spec", index)
}
}
// cachedItems 保存第二次调用使用的商品列表,验证命中跨请求缓存。
cachedItems := make([]mtop.ItemListItem, 8)
// index 表示缓存测试商品下标。
for index := range cachedItems {
cachedItems[index].ID = fmt.Sprintf("probe-%d", index)
}
// err 表示第二次商品多规格探测错误。
if err := srv.enrichSyncedItemMultiSpec(context.Background(), client, "unb=1; _m_h5_tk=t_1;", "acc1", cachedItems); err != nil {
t.Fatalf("cached enrich multi spec: %v", err)
}
if probeCalls != 8 {
t.Fatalf("cached probe calls=%d want 8", probeCalls)
}
// flags、err 保存数据库批量标记查询结果及错误,确认本地查询未误报候选商品。
flags, err := store.Items.MultiSpecFlags(context.Background(), "acc1", []string{"probe-0"})
if err != nil || len(flags) != 0 {
@@ -685,6 +701,24 @@ func TestEnrichSyncedItemMultiSpecLimitsProbeConcurrency(t *testing.T) {
}
}
// TestItemSpecCacheExpires 验证商品多规格缓存过期后不会继续返回旧值。
func TestItemSpecCacheExpires(t *testing.T) {
// srv 是仅用于验证商品多规格缓存生命周期的服务实例。
srv := &Server{itemSpecCache: map[string]itemSpecCacheEntry{
itemSpecCacheKey("acc1", "item-1"): {isMultiSpec: true, expiresAt: time.Now().Add(-time.Second)},
}}
// value、ok 保存过期缓存的读取结果。
value, ok := srv.cachedItemSpec("acc1", "item-1")
if ok || value {
t.Fatalf("expired cache should miss: value=%v ok=%v", value, ok)
}
srv.cacheItemSpec("acc1", "item-1", false)
value, ok = srv.cachedItemSpec("acc1", "item-1")
if !ok || value {
t.Fatalf("fresh false cache mismatch: value=%v ok=%v", value, ok)
}
}
// TestSyncItemsFromAccountFail mtop 返回非成功 → 502。
func TestSyncItemsFromAccountFail(t *testing.T) {
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
+5
View File
@@ -85,6 +85,10 @@ type Server struct {
reconciliation *reconciliation.Service
// transactionRepository 提供统一事务执行所需的最小持久化能力。
transactionRepository transactionRepository
// itemSpecCacheMu 保护商品多规格探测缓存。
itemSpecCacheMu sync.Mutex
// itemSpecCache 保存短期商品多规格探测结果,减少重复远端请求。
itemSpecCache map[string]itemSpecCacheEntry
publishMu sync.Mutex
publishCancels map[string]publishBatchWorker
@@ -154,6 +158,7 @@ func New(store *db.Store, manager *account.Manager, secure bool, webDir, addr st
qrPersisted: make(map[string]qrLoginPersistence),
qrOwners: make(map[string]qrLoginOwner),
loginLimiter: newLoginFailureLimiter(),
itemSpecCache: make(map[string]itemSpecCacheEntry),
}
// option 表示当前遍历过程中的option
for _, option := range options {