resume-server / src /services /pdfCache.js
hari7775's picture
Claude Sonnet 5.5
refactor: split index.js into modules; render Extracurricular Activities in PDFs
12d3f0e
Raw History Blame Contribute Delete
847 Bytes
import crypto from 'crypto';
import { MAX_CACHE_ITEMS } from '../config.js';
// High-speed in-memory cache of pre-rendered PDF buffers (SHA-256 key -> { buffer, createdAt }).
// Eviction is oldest-inserted-first once MAX_CACHE_ITEMS is reached.
const pdfRamCache = new Map();
export function generateContentHash(content, metadata) {
return crypto.createHash('sha256')
.update(content + JSON.stringify(metadata || {}))
.digest('hex');
}
export function getCachedPdf(key) {
return pdfRamCache.get(key);
}
export function cachePdf(key, buffer) {
if (pdfRamCache.size >= MAX_CACHE_ITEMS) {
const oldestKey = pdfRamCache.keys().next().value;
pdfRamCache.delete(oldestKey);
}
pdfRamCache.set(key, { buffer, createdAt: Date.now() });
}
export function cacheSize() {
return pdfRamCache.size;
}