hash
hash2mo ago

Invalidating useQuery cache on refetch

Hey, I'm working on a language learning app, it pulls 10 questions from my convex DB for each quiz game. However I noticed the questions are cached, so restarting the game just reshuffles the 10 or so questions it pulls at the start, and creating a new game doesn't actually pull a new set of questions which is how it's configured to behave. Anyone know how to invalidate cached queries? I couldn't find my answer in the docs
9 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Clever Tagline
Clever Tagline2mo ago
I'm not familiar with invalidating cached queries. However, it might be good to see your current process. Could you share a code snippet or two from the relevant parts of the app?
hash
hashOP2mo ago
sure
Clever Tagline
Clever Tagline2mo ago
Searching the docs for "cache invalidation," one of the results that came back was this Stack article. Maybe it'll help? https://stack.convex.dev/caching-in
The Ultimate Caching Definition: Invalidation, Optimization, and La...
A cache is a non-authoritative representation of data maintained for performance reasons. Find out what these words mean and when you should care abou...
Clever Tagline
Clever Tagline2mo ago
I suggest trying the search in the docs. It includes results from Stack articles and this Discord server.
hash
hashOP2mo ago
Ah, I didn't see that I was trying to find something in the docs for querying, thanks That's more of an article about cache in general (contextually talking abut redis) I'm looking for invalidation of cache'd queries. I'm not super familiar with the architecture of convex so not sure where to start really, lots of stuff is abstracted away. Below is the useQuery hook which is being used inside a useGameLogic hook for handling all the logic for each quiz game. Each quiz game is just 10 multiple choice questions.
const questions = useQuery(api.questions.getQuestions, {
questionType,
// numberOfQuestions: filters.numberOfQuestions,
});
const questions = useQuery(api.questions.getQuestions, {
questionType,
// numberOfQuestions: filters.numberOfQuestions,
});
The issue is that each time a user starts a quiz, it's just reshuffling the already fetched questions and not pulling a completely fresh set from the DB. That's what I mean by cache. Hope this helps. Let me know if you want to see anything else.
Sara
Sara2mo ago
if you haven't already noticed, the useQuery works as a use memo, if the args hasn't changed, there's no way of sending a new value, one of the tricks I do in this case but please know this is not an official source, I just do some wild things is to have a "useState" with a random key, and pass it with the args like: changingValueOnRequest && myActualArgs ? {myActualArgs}:"skip" this would invoke it every time you click it and maybe pass this key down so it actually invokes this change
Clever Tagline
Clever Tagline2mo ago
That reminds me of something that I think I heard/read: pass the current time to the query. Even though the value is not used, the fact that the current time is always changing will invalidate the cache...I think. Again, this is a slightly fuzzy recollection that I recall reading once or twice, and I've never had a need to put it into practice myself so I can't verify how well it works, but maybe...?
hash
hashOP2mo ago
Yeah apparently you can configure it to take in date-time as an arg or configure a function to give you a deterministic seed based on date-time and pass that as an arg Just waiting for my lead to see my msgs :Waiting:

Did you find this page helpful?