perf: cache auction history summaries

This commit is contained in:
dahyeon committed 2026-06-26 04:56:59 -07:00
1 parent 8567948084
commit b9aaf55193
2 files changed
+141 -6

No files matched your search

@@ -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_REHYDRATE_LOCK_TTL_MS = 5 * 1000;
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 {
matchId: mongoose.Types.ObjectId;
@@ -141,6 +144,17 @@ export interface AuctionHistoryView {
takenAt: Date;
}
interface AuctionHistorySummaryRecord {
sequence: number;
action: AuctionActionType;
takenAt: Date;
}
interface AuctionHistoryListCacheEntry {
entries: AuctionHistoryView[];
expiresAtTimestampMS: number;
}
export type AuctionStateResponse = AuctionDocument | Record<string, unknown>;
export interface AuctionBidDeltaResponse {
@@ -183,6 +197,10 @@ function isMongoDuplicateKeyError(error: unknown): boolean {
@Injectable()
export class AuctionService {
private readonly logger = new Logger(AuctionService.name);
private readonly auctionHistoryListCache = new Map<
string,
AuctionHistoryListCacheEntry
>();
constructor(
private readonly connectionService: ConnectionService,
@@ -551,18 +569,64 @@ export class AuctionService {
throw new BadRequestException('Match does not belong to this community.');
}
const entries = await AuctionHistoryModel.find({ matchId: params.matchId })
.sort({ sequence: -1 })
.limit(100)
.lean()
.exec();
const cacheKey = this.buildAuctionHistoryListCacheKey(params);
const cachedHistory = this.getCachedAuctionHistoryList(cacheKey);
if (cachedHistory) return cachedHistory;
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,
action: entry.action,
actionLabel: this.formatHistoryActionLabel(entry.action),
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(