memoise is a memoisation/caching module for node.js. Results are cached in redis or memory, via a backing store. Redis is used if redis host port is provided in options.redis otherwise it fallback to in memory lru store.
create a cache with max 10000 items(only for lru) and a TTL of 300 seconds
const Memoise = require('memoise')
const memoiser = new Memoise({ max: 10000, maxAge: 300 })
Then wrap your original async function like this
const cached = memoiser.wrap(original)
Now call the wrapper as you would call the original function
await cached(arg1, arg2,...argn)
Creates a new Memoiser and returns it
Wraps a given function and returns a cached version of it. Functions to be wrapped must be async function and not object methods. Sometimes, you may want to use a different value of this inside the caller function.
The arguments are used to create the key. Subsequently, when the wrapped function is called with the same n arguments, it would lookup the key in LRU, and if found, call the callback with the associated data. It is expected that the callback will never modified the returned data, as any modifications of the original will change the object in cache.
The debug interface can be used to see stats and cache efficiency.
The library solve thundering heard problem by leaveraging redis as cache.