npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hdsydsvenskan/cached-lookup

v5.1.0

Published

A cached lookup with background refresh

Downloads

1,807

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