perf: cache auction history summaries
This commit is contained in:
1 parent
8567948084
commit
b9aaf55193
2 files changed
+141
-6
No files matched your search
@@ -183,6 +183,77 @@ describe('AuctionService', () => {
|
|||||||
expect(auctionRedisCommandService.placeBid).not.toHaveBeenCalled();
|
expect(auctionRedisCommandService.placeBid).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('lists auction history with a summary projection and short local cache', async () => {
|
||||||
|
const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(1000);
|
||||||
|
const matchId = new mongoose.Types.ObjectId();
|
||||||
|
const communityId = new mongoose.Types.ObjectId();
|
||||||
|
const match = {
|
||||||
|
communityId: {
|
||||||
|
equals: jest.fn((value) => value.toString() === communityId.toString()),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const historyRows = [
|
||||||
|
{
|
||||||
|
action: 'sold',
|
||||||
|
sequence: 4,
|
||||||
|
takenAt: new Date('2026-06-26T00:00:00.000Z'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
action: 'open-bidding',
|
||||||
|
sequence: 3,
|
||||||
|
takenAt: new Date('2026-06-26T00:00:01.000Z'),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const historyExec = jest.fn().mockResolvedValue(historyRows);
|
||||||
|
const historyLean = jest.fn().mockReturnValue({ exec: historyExec });
|
||||||
|
const historyLimit = jest.fn().mockReturnValue({ lean: historyLean });
|
||||||
|
const historySort = jest.fn().mockReturnValue({ limit: historyLimit });
|
||||||
|
const historySelect = jest.fn().mockReturnValue({ sort: historySort });
|
||||||
|
const historyFind = jest.fn().mockReturnValue({ select: historySelect });
|
||||||
|
const { service } = createService({
|
||||||
|
auctionHistory: {
|
||||||
|
find: historyFind,
|
||||||
|
},
|
||||||
|
match: {
|
||||||
|
findById: jest.fn().mockReturnValue({
|
||||||
|
select: jest.fn().mockResolvedValue(match),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
service.listHistory({ matchId, communityId }),
|
||||||
|
).resolves.toEqual([
|
||||||
|
{
|
||||||
|
action: 'sold',
|
||||||
|
actionLabel: '낙찰 처리',
|
||||||
|
sequence: 4,
|
||||||
|
takenAt: historyRows[0].takenAt,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
action: 'open-bidding',
|
||||||
|
actionLabel: '입찰 열기',
|
||||||
|
sequence: 3,
|
||||||
|
takenAt: historyRows[1].takenAt,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
await service.listHistory({ matchId, communityId });
|
||||||
|
|
||||||
|
expect(historyFind).toHaveBeenCalledTimes(1);
|
||||||
|
expect(historySelect).toHaveBeenCalledWith({
|
||||||
|
_id: 0,
|
||||||
|
action: 1,
|
||||||
|
sequence: 1,
|
||||||
|
takenAt: 1,
|
||||||
|
});
|
||||||
|
expect(historySort).toHaveBeenCalledWith({ sequence: -1 });
|
||||||
|
expect(historyLimit).toHaveBeenCalledWith(100);
|
||||||
|
|
||||||
|
nowSpy.mockReturnValue(3101);
|
||||||
|
await service.listHistory({ matchId, communityId });
|
||||||
|
expect(historyFind).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
|
||||||
it('starts an auction without reserving tournament codes before teams are complete', async () => {
|
it('starts an auction without reserving tournament codes before teams are complete', async () => {
|
||||||
const matchId = new mongoose.Types.ObjectId();
|
const matchId = new mongoose.Types.ObjectId();
|
||||||
const communityId = new mongoose.Types.ObjectId();
|
const communityId = new mongoose.Types.ObjectId();
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ const AUCTION_ACTION_REQUEST_REPLAY_WINDOW_MS = 5 * 60 * 1000;
|
|||||||
const AUCTION_ACTION_REQUEST_LEASE_DURATION_MS = 15 * 1000;
|
const AUCTION_ACTION_REQUEST_LEASE_DURATION_MS = 15 * 1000;
|
||||||
const AUCTION_REHYDRATE_LOCK_TTL_MS = 5 * 1000;
|
const AUCTION_REHYDRATE_LOCK_TTL_MS = 5 * 1000;
|
||||||
const AUCTION_REHYDRATE_LOCK_WAIT_MS = 50;
|
const AUCTION_REHYDRATE_LOCK_WAIT_MS = 50;
|
||||||
|
const AUCTION_HISTORY_LIST_LIMIT = 100;
|
||||||
|
const AUCTION_HISTORY_LIST_CACHE_TTL_MS = 2 * 1000;
|
||||||
|
const AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES = 256;
|
||||||
|
|
||||||
interface StartAuctionParams {
|
interface StartAuctionParams {
|
||||||
matchId: mongoose.Types.ObjectId;
|
matchId: mongoose.Types.ObjectId;
|
||||||
@@ -141,6 +144,17 @@ export interface AuctionHistoryView {
|
|||||||
takenAt: Date;
|
takenAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface AuctionHistorySummaryRecord {
|
||||||
|
sequence: number;
|
||||||
|
action: AuctionActionType;
|
||||||
|
takenAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AuctionHistoryListCacheEntry {
|
||||||
|
entries: AuctionHistoryView[];
|
||||||
|
expiresAtTimestampMS: number;
|
||||||
|
}
|
||||||
|
|
||||||
export type AuctionStateResponse = AuctionDocument | Record<string, unknown>;
|
export type AuctionStateResponse = AuctionDocument | Record<string, unknown>;
|
||||||
|
|
||||||
export interface AuctionBidDeltaResponse {
|
export interface AuctionBidDeltaResponse {
|
||||||
@@ -183,6 +197,10 @@ function isMongoDuplicateKeyError(error: unknown): boolean {
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuctionService {
|
export class AuctionService {
|
||||||
private readonly logger = new Logger(AuctionService.name);
|
private readonly logger = new Logger(AuctionService.name);
|
||||||
|
private readonly auctionHistoryListCache = new Map<
|
||||||
|
string,
|
||||||
|
AuctionHistoryListCacheEntry
|
||||||
|
>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly connectionService: ConnectionService,
|
private readonly connectionService: ConnectionService,
|
||||||
@@ -551,18 +569,64 @@ export class AuctionService {
|
|||||||
throw new BadRequestException('Match does not belong to this community.');
|
throw new BadRequestException('Match does not belong to this community.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = await AuctionHistoryModel.find({ matchId: params.matchId })
|
const cacheKey = this.buildAuctionHistoryListCacheKey(params);
|
||||||
.sort({ sequence: -1 })
|
const cachedHistory = this.getCachedAuctionHistoryList(cacheKey);
|
||||||
.limit(100)
|
if (cachedHistory) return cachedHistory;
|
||||||
.lean()
|
|
||||||
.exec();
|
|
||||||
|
|
||||||
return entries.map((entry) => ({
|
const entries = (await AuctionHistoryModel.find({ matchId: params.matchId })
|
||||||
|
.select({ _id: 0, action: 1, sequence: 1, takenAt: 1 })
|
||||||
|
.sort({ sequence: -1 })
|
||||||
|
.limit(AUCTION_HISTORY_LIST_LIMIT)
|
||||||
|
.lean()
|
||||||
|
.exec()) as AuctionHistorySummaryRecord[];
|
||||||
|
|
||||||
|
const history = entries.map((entry) => ({
|
||||||
sequence: entry.sequence,
|
sequence: entry.sequence,
|
||||||
action: entry.action,
|
action: entry.action,
|
||||||
actionLabel: this.formatHistoryActionLabel(entry.action),
|
actionLabel: this.formatHistoryActionLabel(entry.action),
|
||||||
takenAt: entry.takenAt,
|
takenAt: entry.takenAt,
|
||||||
}));
|
}));
|
||||||
|
this.setCachedAuctionHistoryList(cacheKey, history);
|
||||||
|
return history;
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildAuctionHistoryListCacheKey(params: {
|
||||||
|
matchId: mongoose.Types.ObjectId;
|
||||||
|
communityId: mongoose.Types.ObjectId;
|
||||||
|
}): string {
|
||||||
|
return `${params.communityId.toString()}:${params.matchId.toString()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCachedAuctionHistoryList(
|
||||||
|
cacheKey: string,
|
||||||
|
): AuctionHistoryView[] | null {
|
||||||
|
const cached = this.auctionHistoryListCache.get(cacheKey);
|
||||||
|
if (!cached) return null;
|
||||||
|
if (cached.expiresAtTimestampMS <= Date.now()) {
|
||||||
|
this.auctionHistoryListCache.delete(cacheKey);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return cached.entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
private setCachedAuctionHistoryList(
|
||||||
|
cacheKey: string,
|
||||||
|
entries: AuctionHistoryView[],
|
||||||
|
): void {
|
||||||
|
if (
|
||||||
|
!this.auctionHistoryListCache.has(cacheKey) &&
|
||||||
|
this.auctionHistoryListCache.size >=
|
||||||
|
AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES
|
||||||
|
) {
|
||||||
|
const oldestKey = this.auctionHistoryListCache.keys().next().value as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
|
if (oldestKey) this.auctionHistoryListCache.delete(oldestKey);
|
||||||
|
}
|
||||||
|
this.auctionHistoryListCache.set(cacheKey, {
|
||||||
|
entries,
|
||||||
|
expiresAtTimestampMS: Date.now() + AUCTION_HISTORY_LIST_CACHE_TTL_MS,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async openBidding(
|
public async openBidding(
|
||||||
|
|||||||
Reference in new issue
Block a user