{"success":true,"name":"decide-cache-invalidation-before-adding-any-cache","description":"About to cache data for speed, or users report seeing stale/old data.","category":"ai-brimind","path":"ai-brimind/decide-cache-invalidation-before-adding-any-cache/SKILL.md","file":"SKILL.md","content":"---\nname: decide-cache-invalidation-before-adding-any-cache\ndescription: \"About to cache data for speed, or users report seeing stale/old data.\"\n---\n\n# Decide Cache Invalidation Before Adding Any Cache\n\n## When to use\nA cache is being introduced (in-memory, localStorage, HTTP, query cache), and when/how it updates after the source changes hasn't been designed.\n\n## Example request\nChannel list cached for speed → admin updates it; users see the old list for days — cache with no expiry or refresh trigger. Data cached in three layers (server, HTTP, frontend) → a fix clears one; the others keep serving stale — uncoordinated cache layers. Cache cleared 'sometimes' by restart → correctness depends on deploy frequency — accidental invalidation policy.\n\n## Instructions\nAct like a developer who knows a cache is a copy that starts lying the moment the original changes — the design question is never 'how to cache' but 'how does this copy learn the truth again?'. Before caching anything: (1) answer the staleness question in writing: how out-of-date is ACCEPTABLE for this data? Seconds, hours, never? If 'never', reconsider caching it at all; (2) pick the invalidation strategy to match: TTL/expiry (simple, allows bounded staleness), explicit invalidation on write (the code that changes the source clears/updates the cache in the same operation — find every write path, that's the hard part), or versioned keys (change the key, old cache becomes irrelevant); (3) inventory the LAYERS — browser HTTP cache, frontend memory/localStorage, server memory, database — because clearing one layer while three others hold copies fixes nothing; state which layers exist and who invalidates each; (4) keep a manual purge path for emergencies. Only add the cache once you can answer, per layer: 'the source just changed — when and how does this copy find out?'\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1171\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}