Concept Flow - Cache debugging tools
Start Request
Check Cache
Return
Serve
Return Response
This flow shows how a request checks the cache first, returns cached data if found, or fetches fresh data and updates the cache if not.
import cache from 'next/cache'; async function getUserData() { let data = cache.get('user_123'); if (!data) { const fresh = await fetchUser(); cache.set('user_123', fresh); data = fresh; } return data; }
| Step | Action | Cache State | Result | Next Step |
|---|---|---|---|---|
| 1 | Call cache.get('user_123') | Empty | No data found | Fetch fresh data |
| 2 | Call fetchUser() | Empty | User data fetched | Update cache |
| 3 | Call cache.set('user_123', fresh) | Empty -> Has user_123 | Cache updated | Return data |
| 4 | Return data to caller | Has user_123 | Data served | End |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| data | undefined | undefined | undefined | user object | user object |
| cache | empty | empty | empty | contains user_123 | contains user_123 |
Cache debugging tools in Next.js: - Use cache.get(key) to check cached data - If missing, fetch fresh data - Use cache.set(key, data) to update cache - Cache hit returns data immediately - Helps improve performance by avoiding repeated fetches