An Oompa middleware to cache task responses
It is based on the amazing lru-cache package.
const Oompa = require('oompa');
const cache = require('oompa-cache');
const db = require('./my-db');
const server = new Oompa({
GET: ({key}) => db.get(key),
SET: data => {
data[cache.invalidate].GET(data.key);
return db.set(data.key, data.value);
},
});
server.use(cache('GET', 50, ({key}) => key));
server.listen(9000);
To cache common task responses.
- type is the task type you wish to cache.
- cacheOptions is passed to the LRU cache (see here).
- hasher will be called with the request payload and should produce a string key that will be used for the cache.
Every request will recieve a new payload property: require('oompa-cache').invalidate
. The value of this property is an object
with a method for each cached task type (so if we cache every FOO
request, every request will have request.payload[cache.invalidate].FOO
). This method accepts a key, and removes it from that task's cache.
Moreover, if there's a cache hit, no more middleware will be called, so it is advised to use this middleware as the last
of the middleware chain (the last use
call).