W
Wasp-lang•4w ago
sim_sima_

Caching requests

I'm returning signed audio urls from cloudflare, wanted to know if there was a way with useQuery to cache that, and not rehit the API
2 Replies
kapa.ai
kapa.ai•4w ago
Hi @simsima. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To address your question about caching signed audio URLs from Cloudflare using useQuery in Wasp, I can provide some relevant information based on Wasp's query caching capabilities. Wasp's useQuery hook, which is built on top of react-query, does offer caching functionality. By default, query results are cached and will be reused without re-fetching from the API unless they become stale or invalidated. You can control the caching behavior of your query using the options parameter of the useQuery hook. For example, you could set a longer staleTime to keep the data fresh for a longer period:
useQuery(getSignedUrls, {}, {
staleTime: 5 * 60 * 1000 // 5 minutes
})
useQuery(getSignedUrls, {}, {
staleTime: 5 * 60 * 1000 // 5 minutes
})
This would keep the signed URLs in cache for 5 minutes before considering them stale and potentially refetching. If you need more control over the global caching behavior, Wasp allows you to configure the QueryClient in your client setup function:
import { configureQueryClient } from 'wasp/client/operations'

export default async function mySetupFunction() {
configureQueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
},
},
})
}
import { configureQueryClient } from 'wasp/client/operations'

export default async function mySetupFunction() {
configureQueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
},
},
})
}
This example sets the staleTime to Infinity for all queries, effectively caching them indefinitely unless manually invalidated Overriding Default Behaviour for Queries (https://wasp-lang.dev/docs/project/client-config#overriding-default-behaviour-for-queries). However, it's important to note that caching signed URLs for too long might pose security risks, as these URLs typically have an expiration time. You'll need to balance caching duration with the expiration time of your signed URLs. For more detailed control over caching and invalidation, you might need to use react-query's more advanced features directly, as Wasp's current automatic cache invalidation is primarily based on entity updates Cache Invalidation (https://wasp-lang.dev/docs/data-model/operations/actions#cache-invalidation). Remember to adjust these caching strategies based on your specific requirements and the expiration time of your Cloudflare signed URLs. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @simsima, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•3w ago
Did you try what the bot suggested? 🙂
Want results from more Discord servers?
Add your server