npm install cache-wrapper
The example below shows how you can use cache-wrapper to iteratively wrap each function in a resource intensive/slow data store object, so that repeat calls to its methods with the same arguments will use a cached response from the first call.
var cacheWrapper = require('cache-wrapper');
var dataStore = {
getThing: function (id) {
// Do something resource intensive
}
};
var proxiedDataStore = cacheWrapper.wrap(dataStore);
var thing = proxiedDataStore.getThing(1);
Alternatively, you may wrap individual functions for more fine-grained control.
var proxiedGetThing = cacheWrapper.wrap(dataStore.getThing);
var thing = proxiedGetThing(1);
A TTL can be set on caches to ensure that cached data doesn't go stale.
var proxiedDataStore = cacheWrapper.wrap(dataStore, { ttl: 5000 }); // Keep for 5000ms