superagent-cache
Superagent with built-in tiered caching using cache-service.
Basic Usage
Require and instantiate superagent-cache as follows:
var superagent = require('superagent');
require('superagent-cache')(superagent);
Now you're ready for the magic! All of your existing GET
requests will be cached with no extra bloat in your queries!
superagent
.get(uri)
.end(function (err, response){
}
);
Enjoy!
Where does superagent-cache store data?
superagent-cache depends on cache-service to manage caches and store and retrieve data. cache-service supports any type of cache that has been wrapped in its interface (redis and node-cache wrappers are provided by default). See cache-service's docs for the complete API. See the More Examples section for more detailed examples on caching specifics.
Install
npm install superagent-cache
Run Tests
npm test
API
require('superagent-cache')([superagent, cacheServiceConfig])
All params here are optional. If the superagent
param is empty or falsy, then the require statement will return a brand new, patched instance of superagent.
Arguments
- (optional) superagent: an instance of superagent
- (optional) cacheServiceConfig: an object that matches one of the following:
- {cacheService: an instance of cache-service}
- the same object you would pass to cache-service's constructor
.get(uri)
Same as superagent except that superagent's response object will be cached.
.put(uri), .del(uri)
Same as superagent except that the generated cache key will be automatically invalidated when these HTTP verbs are used.
.end(callback ([err,] response [, key]))
Same as superagent except it optionally exposes the key superagent-cache generates as the third param in the callback's argument list. See the usage example for a more detailed explanation.
.responseProp(prop)
If you know you want a single, top-level property from superagent's response object, you can optimize what you cache by passing the property's name here. When used, it causes the .end() function's response to return superagent's response[prop].
Arguments
Example
superagent
.get(uri)
.responseProp('body')
.end(function (error, response){
}
);
.prune(callback (response))
If you need to dig several layers into superagent's response, you can do so by passing a function to .prune(). Your prune function will receive superagent's response and should return a truthy value or null.
Arguments
- callback: a function that accepts superagent's response object and returns a truthy value or null
Example
var prune = funtion(r){
return (r && r.ok && r.body && r.body.user) ? r.body.user : null;
}
superagent
.get(uri)
.prune(prune)
.end(function (error, response){
}
);
.pruneParams(params)
In the event that you need certain query params to execute a query but cannot have those params as part of your cache key (useful when security or time-related params are sent), use .pruneParams() to remove those properties. Pass .pruneParams() an array containing the param keys you want omitted from the cache key.
Arguments
Example
superagent
.get(uri)
.query(query)
.pruneParams(['token'])
.end(function (error, response){
}
);
.pruneOptions(options)
This function works just like the .pruneParams() funciton except that it modifies the arguments passed to the .set() chainable method rather than those passed to the .query() chainable method.
Arguments
- options: array of strings
Example
superagent
.get(uri)
.set(options)
.pruneOptions(['token'])
.end(function (error, response){
}
);
.expiration(seconds)
Use this function when you need to override all of your caches' defaultExpiration
properties (set via cache-service) for a particular cache entry.
Arguments
.cacheWhenEmpty(bool)
Tell superagent-cache whether to cache the response object when it's false
, null
, or {}
.This is especially useful when using .responseProp() or .prune() which can cause response to be falsy. By default, cacheWhenEmpty is true.
Arguments
- bool: boolean, default: true
.doQuery(bool)
Tell superagent-cache whether to perform an ajax call if the generated cache key is not found. By default, cacheWhenEmpty is true.
Arguments
- bool: boolean, default: true
._end(callback (err, response))
This is a convenience method that allows you to skip all caching logic and use superagent as normal.
Arguments
- callback: a function that accepts superagent's error and response objects
.cacheService
If you don't have an external reference to superagent-cache's underlying cache-service instance, you can always get to it this way in case you need to manually add/invalidate keys you get from sources other than superagent queries.
Example
superagent.cacheService...
More Usage Examples
.end() callback argument list options
As an optional parameter in the .end(cb)
callback argument list, superagent-cache can give you the key it generated for each query as follows:
superagent
.get(uri)
.end(function (err, response, key){
console.log('GENERATED KEY:', key);
}
);
This can be useful if you need external access to a cache key and for testing purposes.
However, you can only get it when you pass 3 params to the callback's argument list. The following rules will apply when listing arguments in the .end(cb)
callback argument list:
- 1 param: the param will always be
response
- 2 params: the params will always be
err
and response
- 3 params: the params will always be
err
, response
, and key
Various ways of requiring superagent-cache
When no params are passed
var superagent = require('superagent-cache')();
When only superagent
is passed
var superagent = require('superagent');
require('superagent-cache)(superagent)
When only cacheServiceConfig
is passed
var cacheServiceConfig = {
cacheServiceConfig: {},
cacheModuleConfig: [
{type: 'node-cache', defaultExpiration: 1600},
]
}
var superagent = require('superagent-cache')({}, cacheServiceConfig);
Using cacheServiceConfig
As an instance of cache-service
Here, you have to require and instantiate cache-service yourself, but that means you get an external reference to it if needed.
var cs = require('cache-service').cacheService;
var cacheService = new cs({verbose: true}, [
{type: 'node-cache', defaultExpiration: 1600},
{type: 'redis', redisEnv: 'REDISCLOUD_URL', defaultExpiraiton: 2000}
]);
var cacheServiceConfig = {cacheService: cacheService};
var superagent = require('superagent-cache')(null, cacheServiceConfig);
As a config object
Here, superagent-cache takes care of requiring and instantiating cache-service for you, but you have no external reference to it (although it is accessible via superagent.cacheService
).
var cacheServiceConfig = {
cacheServiceConfig: {verbose: true},
cacheModuleConfig: [
{type: 'node-cache', defaultExpiration: 1600},
{type: 'redis', redisEnv: 'REDISCLOUD_URL', defaultExpiraiton: 2000}
]
}
var superagent = require('superagent-cache')(null, cacheServiceConfig);
More coming soon.
Roadmap
Make it so superagent-cache's .end()
callback function does not require an err
paramMake sure that resetProps()
gets called when ._end()
is called directlyAdd unit tests for the various ways headers can be added to calls- Add unit tests for the other points above
Add the 'More Usage Examples' section- Add thorough comments and param descriptions to the code
- Enable the ability to use cache-service's
postApi
caching API