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:
@@ -1,3 +1,4 @@
|
||||
import { PublicCacheService } from '../public/public-cache.service';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { SyncService } from './sync.service';
|
||||
@@ -27,6 +28,7 @@ describe('SyncService family hooks', () => {
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [
|
||||
PublicCacheService,
|
||||
SyncService,
|
||||
{
|
||||
provide: SdsClientService,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { PublicCacheService } from '../public/public-cache.service';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import {
|
||||
@@ -25,6 +26,7 @@ describe('SyncService', () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
imports: [ConfigModule.forRoot({ isGlobal: true })],
|
||||
providers: [
|
||||
PublicCacheService,
|
||||
SyncService,
|
||||
{ provide: SdsClientService, useValue: sdsMock },
|
||||
{ provide: FamilyRecomputeService, useValue: { enqueue: jest.fn() } },
|
||||
@@ -295,7 +297,7 @@ describe('SyncService product detail scopes', () => {
|
||||
const familyRecompute = {
|
||||
enqueue: jest.fn(),
|
||||
} as unknown as FamilyRecomputeService;
|
||||
const scopedService = new SyncService(prisma, sds, familyRecompute);
|
||||
const scopedService = new SyncService(prisma, sds, familyRecompute, new PublicCacheService());
|
||||
jest
|
||||
.spyOn(scopedService as any, 'persistProductDetail')
|
||||
.mockResolvedValue(undefined);
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
} from './sds-client.service';
|
||||
import { normalizeProductDetail } from './sds-product-detail.mapper';
|
||||
import { FamilyRecomputeService } from '../product-families/family-recompute.service';
|
||||
import { PublicCacheService } from '../public/public-cache.service';
|
||||
|
||||
|
||||
export interface CategorySyncResult {
|
||||
@@ -96,6 +97,7 @@ export class SyncService {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly sds: SdsClientService,
|
||||
private readonly familyRecompute: FamilyRecomputeService,
|
||||
private readonly publicCache: PublicCacheService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -298,6 +300,8 @@ export class SyncService {
|
||||
message: `inserted=${inserted} updated=${updated} total=${flat.length} staleDeleted=${deletedStale}`,
|
||||
},
|
||||
});
|
||||
// 分类树/树序元数据变化 → public 缓存失效(全局约束 §2:不等 TTL)
|
||||
this.publicCache.bump('meta', 'goods');
|
||||
return { inserted, updated, total: flat.length, deletedStale };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
@@ -407,6 +411,8 @@ export class SyncService {
|
||||
message: `inserted=${inserted} updated=${updated} total=${total} delisted=${delistedCount} reactivated=${reactivatedCount} leafCategories=${leafRows.length}`,
|
||||
},
|
||||
});
|
||||
// 链接镜像(名称/图/价/上下架)变化 → public 商品列表/详情失效
|
||||
this.publicCache.bump('goods');
|
||||
return {
|
||||
inserted,
|
||||
updated,
|
||||
@@ -677,6 +683,8 @@ export class SyncService {
|
||||
},
|
||||
});
|
||||
});
|
||||
// 详情/变体/价格落库 → public 详情缓存失效;族矩阵变化由重算钩子 bump matrix
|
||||
this.publicCache.bump('goods');
|
||||
// 族成员的详情/变体变化 → 异步重算该族(进程内去重)
|
||||
await this.maybeEnqueueFamilyRecompute(originGoodId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user