Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oakfang/oompa-cache
An Oompa middleware to cache task responses
https://github.com/oakfang/oompa-cache
Last synced: 6 days ago
JSON representation
An Oompa middleware to cache task responses
- Host: GitHub
- URL: https://github.com/oakfang/oompa-cache
- Owner: oakfang
- License: mit
- Created: 2016-07-10T21:28:43.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-07-10T22:15:11.000Z (over 8 years ago)
- Last Synced: 2024-03-25T01:44:18.165Z (10 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oompa-cache
An Oompa middleware to cache task responsesIt is based on the amazing [lru-cache](https://www.npmjs.com/package/lru-cache) package.
## Usage
```js
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);
```## Why?
To cache common task responses.### `require('oompa-cache')(type:String, cacheOptions:Either, hasher:(Object=>String))`
- **type** is the task type you wish to cache.
- **cacheOptions** is passed to the LRU cache (see [here](https://www.npmjs.com/package/lru-cache#options)).
- **hasher** will be called with the request payload and should produce a string key that will be used for the cache.### Side Effects
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).