cached-lookup v5.4.0
CachedLookup: A Simple Package To Cache And Save On Expensive Lookups & Operations.
Motivation
This package aims to simplify the task of implementing a short-lived caching system for an endpoint which may be calling another third party API under the hood with a usage/rate limit. This package can also help to alleviate pressure when consuming data from databases or I/O network operations by implementing a short lived cache that does not scale relative to incoming requests.
Features
- Simple-to-use API
- TypeScript Support
- Dynamic Cache Consumption
- CPU & Memory Efficient
- No Dependencies
Installation
CachedLookup can be installed using node package manager (npm)
npm i cached-lookupHow To Use?
Below is a small snippet that shows how to use a CachedLookup instance.
const CachedLookup = require('cached-lookup');
// Create a cached lookup instance which fetches music concerts from different cities on a specific date
const ConcertsLookup = new CachedLookup(async (country, state, city) => {
// Assume that the function get_city_concerts() is calling a Third-Party API which has a rate limit
const concerts = await get_city_concerts(country, state, city);
// Simply return the data and CachedLookup will handle the rest
return concerts;
});
// Create some route which serves this data with a 10 second intermittent cache
webserver.get('/api/concerts/:country/:state/:city', async (request, response) => {
// Retrieve the city value from the request - assume there is user validation done on this here
const { country, state, city } = request.path_parameters;
// Retrieve data from the CachedLookup with the cached() and pass the city in the call to the lookup handler
// Be sure to specify the first parameter as the max_age of the cached value in milliseconds
// In our case, 10 seconds would be 10,000 milliseconds
const concerts = await ConcertsLookup.cached(1000 * 10, country, state, city);
// Simply return the data to the user
// Because we retrieved this data from the ConcertsLookup with the cached() method
// We can safely assume that we will only perform up to 1 Third-Party API request per city every 10 seconds
return response.json({
concerts
});
});CachedLookup
Below is a breakdown of the CachedLookup class.
Constructor Parameters
new CachedLookup(Function: lookup): Creates a new CachedLookup instance with defaultoptions.new CachedLookup(Object?: options, Function(...arguments): lookup): Creates a new CachedLookup instance with customoptions.optionsObject: Constructor options for this instance.lookupFunction: Lookup handler which is called to get fresh values.- Note! this callback can be either
synchronousorasynchronous. - Note! you must
return/resolvea value through this callback for the caching to work properly. - Note!
argumentspassed to the methods below will be available in each call to thislookuphandler.
- Note! this callback can be either
CachedLookup Properties
| Property | Type | Description |
|---|---|---|
lookup | function(...arguments) | Lookup handler of this instance. |
cache | Map<string, ValueRecord> | Internal map of cached values. |
promises | Map<string, Promise<T>> | Internal map of promises for pending lookups. |
CachedLookup Methods
cached(Number: max_age, ...arguments): Returns thecachedvalue for the provided set ofargumentsfrom the lookup handler. Automatically falls back to afresh()value if no cached value within themax_ageis available.- Returns a
Promisewhich is resolved to thecachedvalue with a fall back to thefreshvalue. - Note the parameter
max_ageshould be aNumberinmillisecondsto specify the maximum acceptable cache age. - Note this method will automatically fall back to a
fresh()call if no viable cache value is available. - Note the returned
Promisewill reject when the lookup handler also rejects. - Note the provided
argumentsafter themax_agewill be available inside of thelookuphandler function. - Note this method should be used over
rolling()if you want to ensure cache freshness within themax_agethreshold at the sacrifice of increased latency whenever afresh()is resolved to satify themax_agerequirement.
- Returns a
rolling(Number: target_age, ...arguments): Returns thecachedvalue for the provided set ofargumentsfrom the lookup handler. Instantly resolves the most recently cached value while triggering afresh()value call in the background to reload the cache on a rolling basis according to thetarget_age.- Note this method has the same signature as the
cached()method above. - Note this method should be used over
cached()if you want to maintain low latency at the sacrifice of guaranteed cache freshness.
- Note this method has the same signature as the
fresh(...arguments): Retrieves thefreshvalue for the provided set of arguments from the lookup handler.- Returns a
Promisewhich is resolved to thefreshvalue.
- Returns a
get(...arguments): Returns thecachedvalue for the provided set of arguments if one exists in cache.- Returns the
cachedvalue orundefined.
- Returns the
expire(...arguments): Expires thecachedvalue for the provided set of arguments.- Returns a
Booleanwhich specifies whether acachedvalue was expired or not.
- Returns a
in_flight(...arguments): Checks whether afreshvalue is currently being resolved for the provided set of arguments.- Returns a
Booleanto specify the result.
- Returns a
updated_at(...arguments): Returns the last value updatetimestampin milliseconds for the provided set of arguments.- Returns a
Numberorundefinedif no cached value exists.
- Returns a
clear(): Clears all the cached values and resets the internal cache state.- Note the
...argumentsare optional but must be of the following types:Boolean,Number,Stringor anArrayof these types.
CachedLookup Events
fresh: Thefreshevent is emitted whenever a fresh value is retrieved from thelookupfunction with a given set of arguments.- Example:
CachedLookup.on('fresh', (value, arg1, arg2, arg3) => { /* Your Code */ });
- Example:
purge: Thepurgeevent is emitted whenever a stale cache value is purged from the cache.- Example:
CachedLookup.on('purge', (value, arg1, arg2, arg3) => { /* Your Code */ });
- Example:
ValueRecord Properties
| Property | Type | Description |
|---|---|---|
value | T (Generic) | The cached value. |
max_age | undefined | Number | The smallest known max_age of value. |
updated_at | Number | Timestamp (In milliseconds) of when this value was cached. |
License
1 year ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago