#revalidateTag: Bust cache for a single entry?

17 messages · Page 1 of 1 (latest)

thorny dune
#

I have a cache set up like

const getUser = unstableCache(
  (userId) => getUserFromDb(userId), 
  ["user"]
);

how can i bust the cache for ONE user? revalidateTag("user") either busts nothing, or busts the cache for all users.

dull dustBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

unkempt pine
#

note that in your case, you are putting the user inside the key array, which is not the tag array, so revalidateTag("user") doesn't work.

thorny dune
#

I see, thanks!

#

I thought this was done by Next? From the docs:

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.
#

so isn't adding these keys redudant?

unkempt pine
#

just think of it this way: if any strings inside the keypart array changes, then the cache is invalidated (basically)

#

iirc the function's string form (Function.toString() is an implicit key in that keypart array

#

to tag a query for revalidateTag, you need to use the tag array not the keypart array

dull dustBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1238073601962278992 message)

thorny dune
#

sounds a bit funky... Most of the times i want to revalidate based on the inputs of the function. your construction probably orks (still testing it),.... but its heck a verbose...

unstable_cache query keys should have an api like react-query (i.e.: string[] | args => string[])

unkempt pine
#

Well… it’s unstable for a reason. The API can change when it becomes stable

#

For now, this is what we are stuck with and have to use

thorny dune
#

Got it. Let’s see when it becomes stable 😂

#

Thanks for the help