Query System
Learn about SoulCache's query management and data fetching
Query System
The Query System is responsible for orchestrating data fetching, caching, and state management.
Core Concepts
Query Keys
Query keys uniquely identify queries and enable automatic caching:
Query Functions
Query functions fetch your data:
Query States
Queries go through several states:
| State | Description |
|---|---|
idle | Initial state, no fetch in progress |
loading | Currently fetching for the first time |
success | Data available |
error | Fetch failed |
fetching | Background refetch in progress (stale data available) |
The fetchStatus property tracks background refetching separately from the main status.
Usage
Basic Fetch
Subscribe to Updates
Manual Cache Update
Invalidation
Deduplication
SoulCache automatically deduplicates concurrent requests:
Best Practices
- Use descriptive query keys - Make keys meaningful for debugging
- Keep keys stable - Don't create new arrays unnecessarily
- Set appropriate stale time - Balance freshness vs performance
- Handle errors gracefully - Always provide error boundaries
- Clean up subscriptions - Unsubscribe when components unmount
Common Mistakes
- Creating new key arrays - This breaks deduplication
- Missing error handling - Always catch fetch errors
- Forgetting cleanup - Unsubscribe to prevent memory leaks
- Over-fetching - Use stale time to reduce requests