koajs / koa

Expressive middleware for node.js using ES2017 async functions

Home Page:https://koajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Caching/Reusing inflight requests

Hyperblaster opened this issue · comments

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://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?

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