Edge or in-memory cache
Updated 10:42
Should the product feed use an edge cache or an in-memory cache? Show me a typed example.
Assistant:
TypeScript
Short answer: cache at the edge for 60 seconds and revalidate in the background. Here is how the two options compare.
| Strategy | Best for | Trade-off |
|---|---|---|
| Edge cache | Many regions, read-heavy pages | Stale for up to the TTL |
| In-memory | One region, low traffic | Cold after every deploy |
| Both | Mixed traffic | Two places to invalidate |
A typed version of the loader:
type Feed = { items: { id: string; title: string }[]; updatedAt: number };export async function readFeed(load: () => Promise<Feed>, ttl = 60_000) { const cached = cache.get("feed"); if (cached && Date.now() - cached.updatedAt < ttl) return cached; const fresh = await load(); cache.set("feed", fresh); return fresh;}Before you ship it:
- Key the cache by locale, not just by path.
- Set a
stale-while-revalidatewindow so readers never wait on a refresh. - Log the hit rate for a week, then tune the TTL.
Good. Purge the feed cache in every region so the correction goes out.
Assistant:
I need your approval before I run this. It clears the cached feed in three regions, and the next request in each one will be slow while it refills.
Input
{ "key": "feed", "regions": [ "eu", "us", "apac" ], "reason": "Editor published a correction"}This tool wants to run. Review the input, then allow or deny it.
AI can make mistakes. Check important info.