Node.js APIs caching made simple!

The JavaScript and Node.js Blog

In this article we discuss how we can easily implement APIs caching in distributed solutions.
A Node.js implementation is described, in concrete the great http-cache-middleware module:

const middleware = require('http-cache-middleware')()
const service = require('restana')()
service.use(middleware)

service.get('/expensive-route', (req, res) => {
  const data = // heavy CPU and networking tasks...
 
  res.setHeader('x-cache-timeout', '1 week')
  res.send(data)
})

But, what is caching?

Acacheis a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere. Acache hitoccurs when the requested data can be found in a cache, while acache missoccurs when it cannot. Cache hits are served by reading data from the cache, which is faster than recomputing a result or reading from a slower data store; thus, the more…

View original post 509 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s