request-promise-cache
Request promise with cache
2.0.0 Breaks backward compatibility
The resolve
d first argument is no longer {response, body, ?error}
but just the body
. But, you can pass in resolveWithFullResponse=true
to the request({..params})
to get the full response
object instead of the body.
Other promise libraries?
By default, this module uses the native javascript Promise
introduced in Node.js 0.12+, however you can use it with others, by passing your own Promise
constructor
// if you want to use bluebird for example// just do this once, somewhere in your app, ideally whatever file loads first, i.e. app.jsvar request = // you dont have to do it again in the same app's other files
Tested with
if you want me to test another one, just add it and make a pull request to the promiseTypes
Usage
var request = ; var query = recordId: 27 ; var queryString = Object;var cacheKey = url + '?' + queryString; var url = 'http://google.com'; ;
Options
All of the original request library's options, plus the following:
cacheKey: string
, the cache key use, typically, it's just the URL, maybe add the query stringcacheTTL: milliseconds
, automatically expire a cache entry after Y number of milliseconds, if used withcacheLimit
, whichever comes first will take precedencecacheLimit: integer
, automatically expire a cache entry after X amount of reads, if used withcacheTTL
, whichever comes first will take precedencefresh: true/false
, delete the cached entry and get a fresh oneqs._: 123456789 /* anything truthy */
, same asfresh
however, this query param will be sent over to the remote server, so it will, most likely, bypass the cache on the other end if there is oneresolveWithFullResponse: true/false
, copied from request-promise options, defaults tofalse
, basically instead of resolving with thebody
, it uses theresponse
, which then you need to doresponse.body
to access thebody
cacheKey
Asynchronous calls with the same If you make 2 or more requests with the same cacheKey
at the same time, and of course, the response comes back within the cacheTTL
of the first request, only 1 request will go out, the rest will wait for it and resolve at the same time.
Extras
On the returned request
object, you can:
request.original
access the original request function,request.defaults()
another request object function generator, which is used exactly like the originalrequest.defaults
but this one will return a promisified request with the caching.request.cache
is the cache instance using, which is anano-cache
instance, say you need torequest.cache.clear()
License
MIT