Menu

Cloudflare Workers caching: store values, not in-flight promises
📰

Cloudflare Workers caching: store values, not in-flight promises

DEV Community: javascript·Teasa team·3 days ago
#zlmSvH0S
#devtojavascript#devto#dev#entries#cache
Reading 0:00
15s threshold

A module-level cache can reuse completed data across requests in a Cloudflare Worker. Sharing a pending operation is a different decision: the work behind that promise may belong to the request that started it. Our rule for Teasa's isolate caches is to share completed values and let concurrent misses perform their own reads.

This article explains that tradeoff, provides a small JavaScript implementation, and tests the case that ordinary cache-hit tests miss: request A's read never completes, but request B still needs an answer.

The tempting promise cache

Deduplicating concurrent work is useful in many JavaScript applications. A typical implementation puts the promise into a map immediately:

const pending = new Map ();

function loadConfig ( key , read ) { if ( ! pending . has ( key )) { pending . set ( key , read ()); } return pending . get ( key ); }

Enter fullscreen mode

Exit fullscreen mode

Request B now depends on the operation request A started.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.