Constructor
new CacheResolver()
In-memory cache. Resolves promises when adding to cache if the key
doesn't exist or has expired.
Parameters:
Name | Type | Description |
---|---|---|
options.expireInSeconds |
number | Default expiry in seconds for all caches. Cache never expires if undefined. Each cache key can override this value by specifying in `resolve` function. |
Example
var CacheResolver = require('cache-resolver');
var cacheResolver = new CacheResolver()
cacheResolver.resolve({
key: 'testKey',
callback: function() {
return Promise.resolve('value');
}
}).then(function(result) {
console.log(result); // `value`
});
Methods
flush()
Removes all cache values.
remove(key)
Removes an individual cache value for the given `key`.
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Unique identifier for cache value. |
resolve()
Retrieve `key` from cache. If the `key` doesn't exist, use the provided
`callback` to retrieve the value and save to cache. The `callback` function
is only executed once if called multiple times in parallel for the same `key`.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options.key |
string | Unique identifier for cache value. | |
options.callback |
CacheResolver~resolveCallback | Used when cache key undefined or expired. | |
options.expireInSeconds |
number |
<optional> |
Number of seconds before expiring cache value. |
Type Definitions
resolveCallback() → {Promise.<*>}
Callback used when cache key does not already exist or has expired.
Returns:
- Type
- Promise.<*>