Caching Questions
Answered
Dandie Dinmont Terrier posted this in #help-forum
Dandie Dinmont TerrierOP
How does caching work? Is it on the client side or server side?
For example, my app reads data from an external database using
Ideally, I want the database read to happen once -- total -- per hour.
For example, my app reads data from an external database using
unstable_cache. Will it be cached per-user, or will it be cached for all users? Would this change if I switched the data fetching to a route action?Ideally, I want the database read to happen once -- total -- per hour.
12 Replies
unstable_cache is indeed a shared cache, across users
Answer
only React "cache" is scoped to the current request (it's more a dedup function than a cache)
By default Next uses static rendering so if the page itself is not rerendered or you set revalidation to 1h, the data will also be fetched only every hour
unstable_cache is more to handle the case where your page is dynamic (rerender on each request) but you want to cache data from the database (eg update only every 5 minutes)
more occasionaly, you may have data shared by a group of users for instance, that's a way to share the data
Dandie Dinmont TerrierOP
@Eric Burel thanks so much for that resource! That's incredibly helpful.
My webapp is dynamic (personalized content for each user) so it sounds like unstable_cache might be a good option.
yep or node-cache if you want to avoid the unstable for now
the advantage of unstable_cache is that you its built-in, and has replication when hosted on Vercel etc.
but node-cache should be enough for most use cases if you want to go to prod right now
Dandie Dinmont TerrierOP
Ok, good to know. Thanks!