what is node LRU cache? Anyone can explain how to implement it? Lets say I have three layers, client-midlayer(handle calls)-backend(mongoDB), and the LRU cache should be implemented in the midlayer.
Would be nice is there is a simple example just showing how it works! Thanks in advance.
There's an example on how to use it within the source repository: https://github.com/isaacs/node-lru-cache/tree/master/test
I'm assuming you want the LRU to persist to MongoDB? If that's the case, you'll need to extend or rewrite the library, as it looks like a simple in-memory LRU cache module at first glance.
You'll also want to consider Redis' sorted set for this. If you have multiple frontend server instances, keeping a single LRU instance per server will lead to them getting out of sync. Redis's sorted sets are a natural fit for this problem and are extremely fast.
You can use a timestamp to keep them ordered by most recent, and the list can be read and updated atomically via transactions. It will definitely suit the purpose of a cache well.