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 {
|
||||
BadRequestException,
|
||||
@@ -14,7 +15,7 @@ describe('CountriesService', () => {
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [CountriesService, PrismaService],
|
||||
providers: [CountriesService, PrismaService, PublicCacheService],
|
||||
}).compile();
|
||||
service = moduleRef.get(CountriesService);
|
||||
prisma = moduleRef.get(PrismaService);
|
||||
|
||||
@@ -6,13 +6,17 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PublicCacheService } from '../public/public-cache.service';
|
||||
import { CreateCountryDto } from './dto/create-country.dto';
|
||||
import { UpdateCountryDto } from './dto/update-country.dto';
|
||||
import { ReorderCountriesDto } from './dto/reorder-countries.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CountriesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly publicCache: PublicCacheService,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.prisma.country.findMany({
|
||||
@@ -30,6 +34,7 @@ export class CountriesService {
|
||||
}),
|
||||
),
|
||||
);
|
||||
this.publicCache.bump('meta');
|
||||
return this.findAll();
|
||||
}
|
||||
|
||||
@@ -43,12 +48,14 @@ export class CountriesService {
|
||||
|
||||
async create(dto: CreateCountryDto) {
|
||||
try {
|
||||
return await this.prisma.country.create({
|
||||
const created = await this.prisma.country.create({
|
||||
data: {
|
||||
countryName: dto.countryName,
|
||||
countryIcon: dto.countryIcon ?? null,
|
||||
},
|
||||
});
|
||||
this.publicCache.bump('meta');
|
||||
return created;
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
@@ -63,13 +70,15 @@ export class CountriesService {
|
||||
async update(id: bigint, dto: UpdateCountryDto) {
|
||||
await this.findOne(id);
|
||||
try {
|
||||
return await this.prisma.country.update({
|
||||
const updated = await this.prisma.country.update({
|
||||
where: { id },
|
||||
data: {
|
||||
countryName: dto.countryName,
|
||||
countryIcon: dto.countryIcon === undefined ? undefined : dto.countryIcon,
|
||||
},
|
||||
});
|
||||
this.publicCache.bump('meta');
|
||||
return updated;
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
@@ -84,7 +93,9 @@ export class CountriesService {
|
||||
async remove(id: bigint) {
|
||||
await this.findOne(id);
|
||||
try {
|
||||
return await this.prisma.country.delete({ where: { id } });
|
||||
const removed = await this.prisma.country.delete({ where: { id } });
|
||||
this.publicCache.bump('meta');
|
||||
return removed;
|
||||
} catch (err) {
|
||||
if (this.isForeignKeyViolation(err)) {
|
||||
throw new BadRequestException(
|
||||
|
||||
Reference in New Issue
Block a user