Make your Node APIs faster with these 6 lines of code

Making things simple
module.exports = function () {
return {
method: ‘GET’,
path: ‘/myGetRequest/{myParam}’,
handler: function (request, reply) {
const requestParam = decodeURIComponent(request.params.myParam);
const result = myActualCodeToGetData(requestParam);
return reply(result);
},
};
};
const cache = require('memory-cache');
module.exports = function () {
return {
method: ‘GET’,
path: ‘/myGetRequest/{myParam}’,
handler: function (request, reply) {
const requestParam= decodeURIComponent(request.params.myParam),
key = 'myGetRequest' + myParam.toLowerCase(),
cachedResult = cache.get(key);
if (cachedResult !== null) {
return reply(cachedResult);
}
const result = myActualCodeToGetData(requestParam);
cache.put(cacheKey, result);
return reply(result);
},
};
};
cache.clear() // deletes all keyscache.del(key) // deletes a particular 
  • All my caching goes for a toss whenever my server restarts?
  • What if my cache key is unique for most of my requests?
  • Do I have enough RAM on my box?
  • If I am sitting behind a load-balancer with many servers, what would be my cache-hit ratio?
  • My App is more UPSERT than read

--

--

--

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhinav Dhasmana

Abhinav Dhasmana

Senior Staff Engineer @freshworks. Ex-McKinsey/Microsoft/Slideshare/SAP, Tech Enthusiast, Passionate about India. Opinions are mine

More from Medium

AWS Lambda + Typescript

Rest vs GraphQL

Electron: Getting Custom Error Messages from IPC Main

Web Application Firewall rate-limiting in TypeScript CDK