wowprompt

Caching plan with keys, expiry and invalidation for slow reads

Designs a caching layer for slow or repeated reads: what to cache, the key format, expiry times, how entries are cleared when data changes, and how to avoid a stampede when a hot key expires, with code for the chosen store. Use it when pages or API calls are slow because the same database query or external call runs again and again.

A table of cacheable reads with key format, expiry and invalidation trigger, code for a read-through cache helper, and a list of risks with the guard for each
The output this prompt produced, unedited.

The prompt

What to give

  • Language and framework: [e.g. Node.js with Express, Python with Django, PHP with Laravel]
  • Cache available: [e.g. Redis, Memcached, in-process memory, CDN]
  • Slow reads: [paste the queries or calls, how often each runs, how long each takes]
  • How the data changes: [e.g. product prices edited by admins a few times a day, stock changes on every order]
  • How stale data may be: [e.g. prices must be live, blog lists can be 10 minutes old]
  • Per-user or shared data: [which reads depend on the signed-in user]

Task

Decide what to cache and write the caching code for it.

Design checklist

  1. Cache only reads that repeat and cost real time; leave fast or unique reads alone.
  2. Key format with a prefix, a version and every parameter that changes the result, such as product:v2:{id}:{currency}.
  3. Expiry per entry, matched to how stale the data may be.
  4. Invalidation: which writes delete or update which keys, written next to the write code.
  5. Stampede guard for hot keys: a short lock or early refresh so one request rebuilds the entry, not hundreds.
  6. Per-user data kept under user-scoped keys, never served to another user.
  7. Behaviour when the cache is down: fall back to the database, with a timeout.
  8. Metrics: hit rate, miss rate and rebuild time.

Output format

  1. | Read | Cache it? | Key format | Expiry | Cleared by |
  2. A read-through helper in the language given, with comments.
  3. The invalidation calls added to each write path.
  4. | Risk | Guard |, covering stale data, stampedes, memory growth and leaking private data.

Guardrails

  • Never cache responses that contain secrets, tokens or another user's personal data under a shared key.
  • Do not cache error responses for the normal expiry; use a much shorter one or none.
  • State any assumption about traffic or data size as an assumption.
caching strategycache invalidationredis cachecache keysapi performancecache stampede

How to run this

Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.

The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.

Related prompts

All productivity