Dev notes 3 min read

Caching generated PowerPoints with a content hash

Regenerating slides with pptxgenjs on every request was slow. Hashing the input and reusing the stored file when nothing changed made the API feel instant.

I had an endpoint that built a .pptx with pptxgenjs from structured content. Correct, but expensive: layout work, binary write, then download. Calling it repeatedly with the same payload was wasteful.

Approach that worked:

  1. Canonicalize the input (stable JSON string or sorted keys).
  2. Hash it — SHA-256 is fine; you only need a cheap fingerprint.
  3. Store { hash, fileBufferOrGridFSId } in MongoDB (or object storage keyed by hash).
  4. On each request: compute hash; if it matches a row, return the stored file; else generate, save, return.

Same content → same hash → no regeneration. Change one bullet → new hash → one rebuild.

CPU and latency dropped hard. The only discipline is invalidating when your template changes even if content does not — bump a template version in the hash input when you ship layout changes.

Click the dimmed area or Close · Escape

Tags

#nodejs#caching#mongodb#performance

Enjoyed this?

Get notified when I publish new articles. No spam, unsubscribe anytime.

Or follow via RSS