MCPcopy Create free account
hub / github.com/modelcontextprotocol/ext-apps / createPdfCache

Function createPdfCache

examples/pdf-server/server.ts:708–889  ·  view source on GitHub ↗
(
  maxTotalBytes: number = CACHE_MAX_TOTAL_BYTES,
)

Source from the content-addressed store, hash-verified

706 * - CACHE_MAX_LIFETIME_MS from creation (absolute timeout)
707 */
708export function createPdfCache(
709 maxTotalBytes: number = CACHE_MAX_TOTAL_BYTES,
710): PdfCache {
711 const cache = new Map<string, CacheEntry>();
712 let totalBytes = 0;
713
714 /** Delete a cache entry and clear its timers */
715 function deleteCacheEntry(url: string): void {
716 const entry = cache.get(url);
717 if (entry) {
718 clearTimeout(entry.inactivityTimer);
719 clearTimeout(entry.maxLifetimeTimer);
720 totalBytes -= entry.data.length;
721 cache.delete(url);
722 }
723 }
724
725 /** Get cached data and refresh the inactivity timer */
726 function getCacheEntry(url: string): Uint8Array | undefined {
727 const entry = cache.get(url);
728 if (!entry) return undefined;
729
730 // Refresh inactivity timer on access
731 clearTimeout(entry.inactivityTimer);
732 entry.inactivityTimer = setTimeout(() => {
733 deleteCacheEntry(url);
734 }, CACHE_INACTIVITY_TIMEOUT_MS);
735
736 // Move to end of insertion order so size-cap eviction is LRU.
737 cache.delete(url);
738 cache.set(url, entry);
739
740 return entry.data;
741 }
742
743 /** Add data to cache with both inactivity and max lifetime timers */
744 function setCacheEntry(url: string, data: Uint8Array): void {
745 // Clear any existing entry first
746 deleteCacheEntry(url);
747
748 // Evict least-recently-used entries until under the byte cap.
749 for (const oldest of cache.keys()) {
750 if (totalBytes + data.length <= maxTotalBytes) break;
751 deleteCacheEntry(oldest);
752 }
753
754 const entry: CacheEntry = {
755 data,
756 createdAt: Date.now(),
757 inactivityTimer: setTimeout(() => {
758 deleteCacheEntry(url);
759 }, CACHE_INACTIVITY_TIMEOUT_MS),
760 maxLifetimeTimer: setTimeout(() => {
761 deleteCacheEntry(url);
762 }, CACHE_MAX_LIFETIME_MS),
763 };
764
765 cache.set(url, entry);

Callers 2

server.test.tsFile · 0.90
server.tsFile · 0.85

Calls 1

deleteCacheEntryFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…