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
