Metadata makes two db queries?
Answered
Atlantic mackerel posted this in #help-forum
Original message was deleted.
Answered by Rafael Almeida
for
multiple calls for the same function in the same request won't result in multiple db queries
fetch this is not an issue because the requests are deduped, but for anything else you will need to use unstable_cache: https://nextjs.org/docs/app/api-reference/functions/unstable_cachemultiple calls for the same function in the same request won't result in multiple db queries
29 Replies
for
multiple calls for the same function in the same request won't result in multiple db queries
fetch this is not an issue because the requests are deduped, but for anything else you will need to use unstable_cache: https://nextjs.org/docs/app/api-reference/functions/unstable_cachemultiple calls for the same function in the same request won't result in multiple db queries
Answer
@Atlantic mackerel oh sorry I mixed up the cache functions, you can use React's cache: https://nextjs.org/docs/app/building-your-application/caching#react-cache-function
this will memoize the function only for the duration of the request
so you don't need to worry about revalidating it
yeah with
unstable_cache you can basically use the same cache stuff from fetchyou use react's
cache when you just want to avoid rerunning the same function from different places for the same requestI think it depends on how you are using redis, in my experience
unstable_cache is a bit unreliablebut its still experimental so they will probably fix it
yeah thats fine
Original message was deleted
yeah the
keyParts is used by next.js to store and find your cached value, the tags are necessary if you want to manually revalidate it with revalidateTagthey don't need to be the same, in your example I would use
["paste", slug] as the keyParts, but the tag is fine as a single stringwhat do you mean you can't access the variable? 🤔
is the variable defined?
ah true you don't have it yet
no thats a perfectly valid question
and I have no idea how to do it with a dynamic variable like that 😅
I recommend avoiding this function until it gets stable, the docs are very lacking at the moment
in this situation I would use the route segment config for the post pages
you can then use react's
cache function to avoid querying the db twiceand
revalidatePath to revalidate a specific page by its pathyeah I just found this in the repo: https://github.com/vercel/next.js/blob/3a923d5eda04cef563b89ec0c244d26d7fa7e5bb/test/e2e/app-dir/app-static/pages/api/unstable-cache-edge.js#L8
it looks like calling
unstable_cache from within the function is a valid use case so this should be fineoh probably not, most db drivers aren't supported on the edge runtime unfortunately
np 

@Rafael Almeida it looks like calling `unstable_cache` from within the function is a valid use case so this should be fine
Devon Rex
yes that is right, i just finally got my code working with unstable_cache. And to revalidate it you can either use revalidateTag, or if you use revalidatePath on the same page where you call unstable_cache it will revalidate it as well
the docs for unstable cache are definetely lacking, but kapa ai helped in #gpt-help so if you don't already use that you should try it out
cool, thanks for confirming it works! I will give it another try in the future 👀
@Atlantic mackerel please do not delete your messages after the question has been resolved. instead just mark the solution, then unfollow the post – no other actions required aside from these two.