Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
cached-lookup
Advanced tools
A Simple Package To Cache And Save On Expensive Lookups & Operations.
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.
CachedLookup can be installed using node package manager (npm
)
npm i cached-lookup
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
});
});
Below is a breakdown of the CachedLookup
class.
new CachedLookup(Function: lookup)
: Creates a new CachedLookup instance with default options
.new CachedLookup(Object?: options, Function(...arguments): lookup)
: Creates a new CachedLookup instance with custom options
.
options
[Object
]: Constructor options for this instance.
auto_purge
[Boolean
]: Whether to automatically purge cache values when they have aged past their last known maximum age.
true
purge_age_factor
[Number
]: The factor by which to multiply the last known maximum age of a cache value to determine the age at which it should be purged.
1.5
lookup
[Function
]: Lookup handler which is called to get fresh values.
synchronous
or asynchronous
.return
/resolve
a value through this callback for the caching to work properly.arguments
passed to the methods below will be available in each call to this lookup
handler.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. |
cached(Number: max_age, ...arguments)
: Returns the cached
value for the provided set of arguments
from the lookup handler. Automatically falls back to a fresh()
value if no cached value within the max_age
is available.
Promise
which is resolved to the cached
value with a fall back to the fresh
value.max_age
should be a Number
in milliseconds
to specify the maximum acceptable cache age.fresh()
call if no viable cache value is available.Promise
will reject when the lookup handler also rejects.arguments
after the max_age
will be available inside of the lookup
handler function.rolling()
if you want to ensure cache freshness within the max_age
threshold at the sacrifice of increased latency whenever a fresh()
is resolved to satify the max_age
requirement.rolling(Number: target_age, ...arguments)
: Returns the cached
value for the provided set of arguments
from the lookup handler. Instantly resolves the most recently cached value while triggering a fresh()
value call in the background to reload the cache on a rolling basis according to the target_age
.
cached()
method above.cached()
if you want to maintain low latency at the sacrifice of guaranteed cache freshness.fresh(...arguments)
: Retrieves the fresh
value for the provided set of arguments from the lookup handler.
Promise
which is resolved to the fresh
value.get(...arguments)
: Returns the cached
value for the provided set of arguments if one exists in cache.
cached
value or undefined
.expire(...arguments)
: Expires the cached
value for the provided set of arguments.
Boolean
which specifies whether a cached
value was expired or not.in_flight(...arguments)
: Checks whether a fresh
value is currently being resolved for the provided set of arguments.
Boolean
to specify the result.updated_at(...arguments)
: Returns the last value update timestamp
in milliseconds for the provided set of arguments.
Number
or undefined
if no cached value exists.clear()
: Clears all the cached values and resets the internal cache state....arguments
are optional but must be of the following types: Boolean
, Number
, String
or an Array
of these types.fresh
]: The fresh
event is emitted whenever a fresh value is retrieved from the lookup
function with a given set of arguments.
CachedLookup.on('fresh', (value, arg1, arg2, arg3) => { /* Your Code */ });
purge
]: The purge
event is emitted whenever a stale cache value is purged from the cache.
CachedLookup.on('purge', (value, arg1, arg2, arg3) => { /* Your Code */ });
Property | Type | Description |
---|---|---|
value | T (Generic) | The cached value. |
max_age | `undefined | Number` |
updated_at | Number | Timestamp (In milliseconds) of when this value was cached. |
FAQs
A Simple Package To Cache And Save On Expensive Lookups & Operations.
The npm package cached-lookup receives a total of 491 weekly downloads. As such, cached-lookup popularity was classified as not popular.
We found that cached-lookup demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.