You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to work on a caching solution for inflight requests to Koa,
Let's say that i spam 100 requests concurrently to the same endpoint
http:https://mykoa.application.com/getresults
in my router middleware is it possible to cache the requests and then return the same result to all of them?
constinflight={};router.use(asyncfunction(ctx,next){// Create a cache 'key' consthash=`${ctx.request.url}-${ctx.state.user?.data?.id}-${JSON.stringify(ctx.request.body)}`;// Check if there is already a request inflightif(inflight[hash]){// If there is then wait for the promise resolutionreturnawaitinflight[hash];}// Cache the request resolution for any other identical requestsinflight[hash]=next();awaitinflight[hash];// Clean it up so that the next request will be freshinflight[hash].then(function(res){deleteinflight[hash];},function(err){deleteinflight[hash];})})
In my head this should work, but it's still running each request afresh synchronously
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to work on a caching solution for inflight requests to Koa,
Let's say that i spam 100 requests concurrently to the same endpoint
in my router middleware is it possible to cache the requests and then return the same result to all of them?
In my head this should work, but it's still running each request afresh synchronously
Beta Was this translation helpful? Give feedback.
All reactions