@hdsydsvenskan/cached-lookup
v5.1.0
Published
A cached lookup with background refresh
Downloads
1,045
Maintainers
Keywords
Readme
Cached Lookup
A cached lookup with background refresh
Installation
npm install --save @hdsydsvenskan/cached-lookup
Release new version
Follow Semantic Versioning and use np and a version like patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3
np patch
Usage
const cachedLookup = require('@hdsydsvenskan/cached-lookup');
// Create a cached lookup...
const getSomeData = cachedLookup(
param1 => doSomethingAsync(param1)
.then(result => processIt(result)),
{
name: 'some-data', // The name that should be used in logs etc to identify this lookup
cacheTime: 60 * 1000, // Cache the returned data for a minute
logger: bunyanCompatibleLogger
}
);
// ...and do the lookup, getting cached lookup back if any has been successfully resolved
getSomeData('foobar')
.catch(...)
.then(...)
API Usage
cachedLookupMethod = cachedLookup(lookupMethod, options)
Overriding the cache on a global level
To be able to override the caching part of the cacheLookup (for example when
running tests in an application that has a dependency on something that in turn
includes this library with no way to disable it) it will also read from the
environment when running. If it finds the DISABLE_CACHED_LOOKUP
variable and
it's set to "true"
then all calls to cacheLookup will simply return the
lookupCallback
itself, effectively skipping the cache.
Lookup Method
The lookup method should return a Promise
that when resolved is cached with the parameters sent into the method as the key and when rejected will trigger a retry according to the defined retry mechanism. After the cache time has expired a new lookup will be made and when successful it will replace the result returned for that set of lookup arguments and when not successful it will be retried until successful.
Options
- [logger] – string – a Bunyan compatible logger. Set to
false
to disable logging. - [name] – string – a name that will be used to identify this lookup in logs. Defaults to
unnamed
. - [cacheTime] – integer – how many milliseconds to cache the result in. After this time a renewal will be attempted in the background on the next fetch while still returning the cached data until some new data has been found. Defaults to
1000
. - [errorWaitTime] – integer – how many base milliseconds to wait before retrying if a fetch attempt has failed. Will be increased by each fail and have a random modifier applied to it to back off politely against the other service. Defaults to
1000
. - [errorWait(errorCount, errorWaitTime)] – function – a function that replaces the built in back-off mechanism. Should return the amount of milliseconds to wait before retrying a fetch after an error. Defaults to the static
errorWait()
method. - [keyGenerator()] – function – called with same arguments as the
lookupMethod
and returns the cache key that should be used for that set of arguments. - [refreshrateCallback(refresh)] – function – called with
true
for every retrieve that triggers a refresh andfalse
for every retrieve that don't.
Static methods
- errorWait(errorCount, errorWaitTime) – the default errorWait method is available so that that it can be wrapped in additional logic and sent in to a cached lookup.