Reputation: 254
i'm wondering if node.js is using cache for the follow scenario or if a module for that is existing:
When you have for example a web-portal which shows you at the startpage 20 products with images, every time the server has to fetch the images from the hdd or in best case from a ssd. for every single image just to find it the server need about 5-7 ms.when you have 50 user are visiting the startpage at the same time it would take 20img * 5ms * 50 usr = 5000ms just to find the images on the hdd.
So it would be nice if is there was a way to keep all often used files like images, css, html and so on in the memory.so you just define the cache size. For example 50MB and the module/node.js keep the often used files in the cache.
Upvotes: 3
Views: 109
Reputation: 1965
Node.js itself (by default) doesn't do any caching, although OS and other lower layer elements (e.g., HDD) may do it, speeding up consecutive reads significantly.
If you want to enable cache'ing http responses in nodejs there is a http-cache
library - https://www.npmjs.com/package/http-cache and request-caching
library - https://www.npmjs.com/package/node-request-caching. For caching files you can use filecache
https://www.npmjs.com/package/filecache and for serving static files - serve-static
(https://www.npmjs.com/package/serve-static).
If you're using a framework such as Express it's not that simple anymore - for example, running Express in production mode causes it to cache some stuff (like templates or CSS). Also note that res.sendFile
streams the file directly to the customer (possibly proxy server, such as nginx)
However, even Express' webpage (http://expressjs.com/en/advanced/best-practice-performance.html) advises to use a separate proxy:
Cache request results
Another strategy to improve the performance in production is to cache the result of requests, so that your app does not repeat the operation to serve the same request repeatedly.
Use a caching server like Varnish or Nginx (see also Nginx Caching) to greatly improve the speed and performance of your app.
For other recommendations about speeding up nodejs you can take a look at https://engineering.gosquared.com/making-dashboard-faster or http://www.sitepoint.com/10-tips-make-node-js-web-app-faster/
Upvotes: 1
Reputation: 2456
The builtin Web server on Nodejs doesn't implement any content cache. Like @prc322 says the specific answer depend on your technology stack on top of Nodejs (framework, middleware, etc.). On the other hand Nginx is more widely used as Web server for static assets. Normally they are combined in some way that the static assets are server from Nginx and the application logic (rest services, etc.) are handled by the Nodejs application. Content caching is part of the HTTP protocol standard and you can benefit of it by setting a HTTP cache like varninsh in front of your Web application.
Upvotes: 0