request-cached-promise
Cache proxy class for request-promise: essentially a combination of request-promise and cache-manager with some hashing logic. See:
- promisified variation of request: https://github.com/request/request-promise
- multilayer flexible cache: https://github.com/BryanDonovan/node-cache-manager
Note: request-promise uses Bluebird for promises, so you get not a ES6 native Promise but a Bluebird one. This should be a little concern, since Bluebird is Promises/A+ complient.
Usage
You can bind the lib to a cache-manager cache and then use, as it were request-promise with all its features.
const rcp = ;const request = rcp; ;
You can bind multiple times to have different caches used for different requests.
const requestMem = rcp;const requestMultilayer = rcp; ;;
Example
... with a simple memory cache, though in real live you might want to use also redis or mongo etc.
const cacheManager = cache = cacheManager request = ; { return ;} ;
How it works
The library takes request first parameter and generates a key out of it. If the param is a string, then the key is the URI itself. Otherwise the key is URI with a hash added at the end. This schema is used for easier debugging of cached responses when they're saved in say mongo.
The example above could produce the following record when using cache-manager-mongoose store:
{
"_id" : "http://some.service/providing/user/john.doe _978826362",
"exp" : ISODate("2016-04-17T11:34:32.853Z"),
"val" : {
"id": "john.doe",
"firstName" : "John",
"lastName": "Doe"
}
}
Here _978826362
is a hash generated from the request param:
Customization
The module exports two functions, which you can override if you want.
Override examples:
const rcp = ; rcp { /* my implementation of hashig, e.g. MD5 */}; rcp { // in my case all requests have unique URI // i don't want to hash request params return requestParamuri || requestParam;};