Next.js Discord

Discord Forum

What is difference between cache from reactjs and unstable_cache from nextjs

Answered
English Angora posted this in #help-forum
Open in Discord
Avatar
English AngoraOP
Hello guys can you tell me difference between cache from reactjs and unstable_cache from nextjs
Answered by B33fb0n3
yea: imagine you fetch posts on two locations on the same page.

Without any cache your database will be requested two times.
And in the future again two times

If you use react cache, the post fetching is memorized for this one request. So it will be requests from your database once and the result will be reused for the other part of your page.
The same happens for future requests.

If you use unstable_cahe, the post data will be written into the data cache and there are two requests to the database (unstable_cache is not dedupped by default).
In the future there won't be more requests to your database, because the cache data is just returned.
View full answer

5 Replies

Avatar
the react cache is not really a cache. It's more a place where you can put data, that will be memorized for one request (clientside).

The unstable_cache (named data cache) is a serverside cache that saves values. It will be the same for other reuqests as well
Avatar
English AngoraOP
can you give example?
Avatar
yea: imagine you fetch posts on two locations on the same page.

Without any cache your database will be requested two times.
And in the future again two times

If you use react cache, the post fetching is memorized for this one request. So it will be requests from your database once and the result will be reused for the other part of your page.
The same happens for future requests.

If you use unstable_cahe, the post data will be written into the data cache and there are two requests to the database (unstable_cache is not dedupped by default).
In the future there won't be more requests to your database, because the cache data is just returned.
Answer
Avatar
English AngoraOP
Thanks friend for help
Avatar
sure thing