Caching/Reusing inflight requests #1712
Answered
by
jonathanong
Hyperblaster
asked this question in
Q&A
-
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? const inflight = {};
router.use(async function(ctx, next) {
// Create a cache 'key'
const hash = `${ctx.request.url}-${ctx.state.user?.data?.id}-${JSON.stringify(ctx.request.body)}`;
// Check if there is already a request inflight
if (inflight[hash]) {
// If there is then wait for the promise resolution
return await inflight[hash];
}
// Cache the request resolution for any other identical requests
inflight[hash] = next();
await inflight[hash];
// Clean it up so that the next request will be fresh
inflight[hash].then(function(res) {
delete inflight[hash];
}, function(err) {
delete inflight[hash];
})
}) In my head this should work, but it's still running each request afresh synchronously |
Beta Was this translation helpful? Give feedback.
Answered by
jonathanong
Aug 29, 2024
Replies: 1 comment
-
you can try this module: https://github.com/koajs/cash/blob/master/index.js |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
jonathanong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can try this module: https://github.com/koajs/cash/blob/master/index.js