perf(public): 公开读路径进程内分域缓存——meta/goods/matrix 版本域 + TTL 兜底 + in-flight 合并

- PublicCacheService:分域版本号失效(bump 即作废,不等 TTL)、loader 期间
  bump 的竞态防护(返回但不回写)、并发 miss 单飞、PUBLIC_CACHE_DISABLED
  /TTL_MS/MAX_ENTRIES 应急开关;@Global 模块
- PublicService 六端点接缓存:列表缓存全量物化(分页切片在缓存外按请求执行,
  修复'所有页返回第一页'的切片缓存错误)、详情/首页/树/标签组/树序元数据/
  族最低价聚合各按依赖域缓存
- 全写路径挂钩 bump:admin CRUD(goods/categories/countries/tags/tag-groups/
  positions/origin-goods 标签)、sync 三同步、族重算、整理全家桶——
  事务提交成功后失效对应域,product-families 经 recompute 天然覆盖
- 测试:PublicCacheService 单测 13 例 + 失效链路集成 8 例(读命中/写后立即可见
  端到端/每条写路径域断言);既有两套件测数据语义改为显式禁缓存
This commit is contained in:
yeuimu
2026-09-03 03:40:20 +08:00
parent ca38476047
commit ab79325ab0
33 changed files with 1777 additions and 927 deletions
@@ -1,3 +1,4 @@
import { PublicCacheService } from './public-cache.service';
import { Test } from '@nestjs/testing';
import { Prisma } from '@prisma/client';
import { PublicService } from './public.service';
@@ -24,8 +25,11 @@ describe('PublicService family block (PUBLIC_DETAIL_FROM_FAMILY)', () => {
let familyId = 0n;
beforeAll(async () => {
// 本套件测数据语义(裸 prisma 改数后立即读公开端点),不走带 bump 的写路径——
// 禁用 public 缓存;缓存命中/失效语义由 public-cache.invalidation.spec.ts 覆盖
process.env.PUBLIC_CACHE_DISABLED = 'true';
const moduleRef = await Test.createTestingModule({
providers: [PublicService, PrismaService],
providers: [PublicService, PrismaService, PublicCacheService],
}).compile();
service = moduleRef.get(PublicService);
prisma = moduleRef.get(PrismaService);
@@ -71,11 +75,12 @@ describe('PublicService family block (PUBLIC_DETAIL_FROM_FAMILY)', () => {
createdFamilyIds.push(family.id);
await prisma.originGood.update({ where: { id: og.id }, data: { familyId: family.id } });
// 解析去运行时化:先整理派生标签,再重算(重算只聚合不解析)
const recomputeSvc = new FamilyRecomputeService(prisma);
const recomputeSvc = new FamilyRecomputeService(prisma, new PublicCacheService());
const organizeSvc = new OrganizeService(
prisma,
recomputeSvc,
new ProductFamiliesService(prisma, recomputeSvc),
new PublicCacheService(),
);
await organizeSvc.deriveFamilyTags(family.id);
await recomputeSvc.recomputeFamily(family.id);
@@ -196,13 +201,14 @@ describe('Good familyId derivation', () => {
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
providers: [PrismaService],
providers: [PrismaService, PublicCacheService],
}).compile();
prisma = moduleRef.get(PrismaService);
await prisma.onModuleInit();
familiesService = new (require('../product-families/product-families.service').ProductFamiliesService)(
prisma,
new FamilyRecomputeService(prisma),
new FamilyRecomputeService(prisma, new PublicCacheService()),
new PublicCacheService(),
);
});