Cache eviction in TanStack Query and SWR: staleTime, gcTime & LRU
You probably use TanStack Query or SWR every day. Start with a familiar puzzle, 'why did React Query refetch this?', and learn how staleTime and gcTime decide when cached data is refetched or dropped. Then trace an LRU cache built from a hash map and a doubly linked list, compare it with LFU and TTL, and see why a memo Map with no size limit leaks memory.
Questions
- Not answered. Why did React Query refetch this?
- Not answered. What's the difference between
staleTimeandgcTime? - Not answered. Which key does the LRU cache evict when
put('D', 4)runs? - Not answered. Why does an LRU cache's list need to be doubly linked?
- Not answered. What happens to this memo
Mapon a long-running server? - Not answered. Which statements about a hash map + doubly linked list LRU cache are true?
- Not answered. Which statements about LRU, LFU, and TTL eviction are true?
- Not answered. What makes TanStack Query refetch a stale query?
- Not answered. Which statements about a plain JS
Mapare true? - Not answered. After how many minutes is an inactive query garbage-collected?