Baterka
Baterka

Reputation: 3704

How to cache specific routes what read from disk in Express.js

I have Express.js API with few routes with static content loaded from the drive (filesystem).

As example: GET /locale?lang=en - Loads en.json from drive and parse it into JSON response.

Does Express.js have some native caching for this kind of requests, or I just should load the language file into a variable (memory) and supply it to requests from memory + Create some system which will automatically refresh in-memory version in case of changes?

Upvotes: 0

Views: 336

Answers (1)

winwiz1
winwiz1

Reputation: 3164

After the file is read once from disk, it likely will be cached by OS e.g. by the filesystem and served from memory next time. Unless there is no enough memory available. In order to increase Express performance, it's important to ensure there is no blocking on disk I/O by performing all disk operations asynchronously. In many practical cases this would be enough for a single disk file. Going beyond that, Express doesn't have built-in caching but it can be implemented using some distributed cache: Redis is easier to use in many cases, memcached offers more Spartan functionality but is more performant.

Upvotes: 1

Related Questions