djbalin
djbalin•3mo ago

Caching subpart of query

Hello 🌅 Context In our video streaming app, we have an expensive query that fetches the home feed for our users. This query fetches the same videos for all our users, and so it is obvious to leverage cache for this.
const data = useQuery(api.videos.getStaticFeed, {}) // can be cached for all users
const data = useQuery(api.videos.getStaticFeed, {}) // can be cached for all users
However, we have implemented a personal content filtering system whereby each individual user can enable/disable certain channels or topics. We must therefore pass the userid to this query and check whether they have any filters set up:
const data = useQuery(api.videos.getStaticFeed, {profileId}) // is not cached since profileId is user-unique
const data = useQuery(api.videos.getStaticFeed, {profileId}) // is not cached since profileId is user-unique
Problem Since we pass a unique argument for every user, our cache is invalidated (and our bandwidth very high). I would like to do this content filtering on the backend and not the frontend. We can not leverage ctx.auth.userIdentity since a user can have multiple profiles and each profile can have their own filters set up. Question We are still fetching the same data for every user in getStaticFeed and then just performing some post-filtering based on the profileId. Is it possible to somehow cache this sub-query within getStaticFeed? If i use ctx.runQuery, would that call be cached? Using that approach is discouraged per docs due to extra overhead, however. Thanks!
1 Reply
Convex Bot
Convex Bot•3mo 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!

Did you find this page helpful?