Skip to content

Commit 30a78b6

Browse files
authored
perf(pdf-server): share cache across server instances and dedupe form parsing (#637)
* perf(pdf-server): share cache across server instances and dedupe form parsing In stateless HTTP deployments createServer() is called per request, so the per-instance pdfCache and the 4 MB viewer HTML were discarded after every call. Hoist both to module scope. Also refactor extractFormSchema/extractFormFieldInfo to accept an already-parsed PDFDocumentProxy so display_pdf downloads and parses the PDF once instead of twice. * perf(pdf-server): cap total cache bytes with LRU eviction The module-level sharedPdfCache could grow unbounded within the 60s lifetime window under a burst of distinct URLs. Track running total bytes and evict least-recently-used entries on insert when it would exceed CACHE_MAX_TOTAL_BYTES (256MB). getCacheEntry now bumps the accessed entry to the end of insertion order so eviction targets the LRU entry rather than the oldest insert. createPdfCache takes an optional maxTotalBytes for testability.
1 parent 7344d4c commit 30a78b6

2 files changed

Lines changed: 169 additions & 120 deletions

File tree

examples/pdf-server/server.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,72 @@ describe("PDF Cache with Timeouts", () => {
7171
});
7272
});
7373

74+
describe("byte-cap LRU eviction", () => {
75+
const tenBytes = new Uint8Array(10);
76+
77+
async function fill(cache: PdfCache, url: string): Promise<void> {
78+
const m = spyOn(globalThis, "fetch").mockResolvedValueOnce(
79+
new Response(tenBytes, { status: 200 }),
80+
);
81+
try {
82+
await cache.readPdfRange(url, 0, 1024);
83+
} finally {
84+
m.mockRestore();
85+
}
86+
}
87+
88+
it("evicts least-recently-used entry when total exceeds cap", async () => {
89+
const cache = createPdfCache(25);
90+
try {
91+
await fill(cache, "https://arxiv.org/pdf/a");
92+
await fill(cache, "https://arxiv.org/pdf/b");
93+
expect(cache.getCacheSize()).toBe(2);
94+
95+
// Touch A so B becomes least-recently-used
96+
await cache.readPdfRange("https://arxiv.org/pdf/a", 0, 1);
97+
98+
// Inserting C (10B) pushes total to 30 > 25 → evict LRU (B)
99+
await fill(cache, "https://arxiv.org/pdf/c");
100+
expect(cache.getCacheSize()).toBe(2);
101+
102+
// A and C still served from cache; B re-fetches
103+
const m = spyOn(globalThis, "fetch").mockResolvedValue(
104+
new Response(tenBytes, { status: 200 }),
105+
);
106+
try {
107+
await cache.readPdfRange("https://arxiv.org/pdf/a", 0, 1);
108+
await cache.readPdfRange("https://arxiv.org/pdf/c", 0, 1);
109+
expect(m).toHaveBeenCalledTimes(0);
110+
await cache.readPdfRange("https://arxiv.org/pdf/b", 0, 1);
111+
expect(m).toHaveBeenCalledTimes(1);
112+
} finally {
113+
m.mockRestore();
114+
}
115+
} finally {
116+
cache.clearCache();
117+
}
118+
});
119+
120+
it("evicts multiple entries if a single insert exceeds the cap", async () => {
121+
const cache = createPdfCache(25);
122+
try {
123+
await fill(cache, "https://arxiv.org/pdf/a");
124+
await fill(cache, "https://arxiv.org/pdf/b");
125+
const big = spyOn(globalThis, "fetch").mockResolvedValueOnce(
126+
new Response(new Uint8Array(20), { status: 200 }),
127+
);
128+
try {
129+
await cache.readPdfRange("https://arxiv.org/pdf/big", 0, 1024);
130+
} finally {
131+
big.mockRestore();
132+
}
133+
expect(cache.getCacheSize()).toBe(1);
134+
} finally {
135+
cache.clearCache();
136+
}
137+
});
138+
});
139+
74140
describe("readPdfRange caching behavior", () => {
75141
const testUrl = "https://arxiv.org/pdf/test-pdf";
76142
const testData = new Uint8Array([0x25, 0x50, 0x44, 0x46]); // %PDF header

0 commit comments

Comments
 (0)