Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hdsydsvenskan/cached-lookup

Package Overview
Dependencies
Maintainers
15
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hdsydsvenskan/cached-lookup

A cached lookup with background refresh

  • 5.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
256
decreased by-45.88%
Maintainers
15
Weekly downloads
 
Created
Source

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 and false 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.

FAQs

Package last updated on 18 Sep 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc