I have an endpoint in express.
app.get('/',(req,res)=>{
Promise.all(fetch(url1),fetch(url2))
.then(i=>res.send(i=))
})
I'd like to make a cache to avoid to make the requests only every 15seconds. Let's says it take 5 seconds to make both fetch.
If I do a simple cache like this:
const cache={};
app.get('/',(req,res)=>{
if(cache['mykey']==undefined
|| cache['mykey'].timestamp>new Date().getTime()-15000){
// make or refresh cache;
Promise.all(fetch(url1),fetch(url2))
.then(i=>{
cache['mykey'].timestamp=new Date().getTime();
cache['mykey'].value=i
return i;
})
.then(i=>res.send(i))
}else{
res.send(cache['mykey'].value);
// or res.status(304).send()
}
}
It will not work if 2 or more request are made during the phase of cache refreshing. How can I make only ONE cache refreshing every 15 minutes ?
Hope I am clear.