Cache Key Registry
Objective
Create a single, typo-safe place for root cache key names and a shared factory for all TanStack Query keys.Files
lib/cache-keys.tslib/query-keys.ts
Rules
Root key names
- every root key name must come from
CACHE_KEYS - keys must be uppercase in the registry
- no hard-coded root key strings outside
lib/cache-keys.tsandlib/query-keys.ts
Query key composition
- all query key arrays must be created in
lib/query-keys.ts - every user-scoped key must include
userIdat index[1] - every domain should expose an
.allkey for prefix invalidation
Naming Guidelines
- use singular domain groups in the factory object when it reads well, but keep root strings aligned with existing cache language
- prefer
list,detail,summary,stats,byId,byDate,unifiedas stable suffix conventions - avoid inventing multiple suffix names for the same pattern
Migration Steps
- Create
CACHE_KEYSwith every root query key currently in use. - Build
globalKeysanduserKeys(userId). - Replace local factories such as
createQueryKeys. - Replace inline key arrays with factory helpers.
- Leave no orphaned root key string arrays behind.
Review Checklist
- does the domain have an
.allkey? - is
userIdat index[1]for user-scoped data? - does the new factory preserve existing cache segmentation where needed?
- are there any inline arrays left in the touched files?