unstable_cache::keyParts and non-primitives
Answered
American posted this in #help-forum
AmericanOP
Hello!
I'm wondering how unstable_cache works in more details.
From the docs: https://nextjs.org/docs/app/api-reference/functions/unstable_cache
I'm wondering how unstable_cache works in more details.
From the docs: https://nextjs.org/docs/app/api-reference/functions/unstable_cache
Bold mine, I wonder; what happens when arguments are objects, how are they compared?keyParts: This is an array that identifies the cached key. It must contain globally unique values that together identify the key of the data being cached. The cache key also includes the arguments passed to the function.
Answered by Chinese Egret
I have not looked into the code yet, but I would think it is like this:
- You pass a function and global unique key.
- You call the returned function.
- Next check if a return value is cached based on the global key and the args you pass to the returned fn.
- Cached? Return that. Otherwise, call the passed fn
- You pass a function and global unique key.
- You call the returned function.
- Next check if a return value is cached based on the global key and the args you pass to the returned fn.
- Cached? Return that. Otherwise, call the passed fn
6 Replies
Chinese Egret
I have not looked into the code yet, but I would think it is like this:
- You pass a function and global unique key.
- You call the returned function.
- Next check if a return value is cached based on the global key and the args you pass to the returned fn.
- Cached? Return that. Otherwise, call the passed fn
- You pass a function and global unique key.
- You call the returned function.
- Next check if a return value is cached based on the global key and the args you pass to the returned fn.
- Cached? Return that. Otherwise, call the passed fn
Answer
Chinese Egret
You do not have to pass args on the key, Next does that to see if a cached value is saved with those params
It’s like when using fetch
Here is the code:
packages/next/src/server/web/spec-extension/unstable-cache.ts
packages/next/src/server/web/spec-extension/unstable-cache.ts
AmericanOP
Thank you! ðŸ™