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

cache-hold

v1.0.5

Published

An in-memory caching module which holds similar calls and fulfils all with the same result. Supports Promises and periodical cache updates.

Downloads

13

Readme

node-cache-hold

An advanced in-memory cache module which allows you to keep hold of calls with the same cache key, avoiding the backend to get overwhelmed by calls in case an item expires. It supports both Promises and callbacks and it's fully written in ES5 for maximum compatibility.

Using it

With Promises/Async-Await

const CacheHold = require('cache-hold');
const cache = new CacheHold({
  ttl: 120 // 2 minutes
});

async function makeHTTPCall() { ... }

const value = await cache.lookup('cache_key', () => makeHTTPCall());

With callbacks

const CacheHold = require('cache-hold');
const cache = new CacheHold({
  ttl: 120 // 2 minutes
});

function makeHTTPCall(callback) { ... }

cache.lookup('cache_key', (callback) => makeHTTPCall(callback), (err, res) => {
  if (err)
    throw new Error('Failed to make HTTP call');
  console.log('HTTP call results: ', res);
});

The lookup() method

The lookup() method is the main (and almost only) method of node-cache-hold.

It's called this way: lookup(cache_key, fetcherFunction[, callback]).

  • The cache_key is the key used to lookup and store the data in cache
  • The fetcherFunction is the function responsible to retrieve the data in cache it's not found in cache; This function is called with one argument, the callback but it can also return a Promise
  • The callback is the function called to return the final value; If a callback is not provided and node has built-in Promise support, the lookup() method will return a Promise instance, which you can await on.

Supported options

The CacheHold() constructor options supports all the following settings:

  • ttl - The time (in seconds) for an item to live in cache; Defaults to Infinity
  • gracePeriod - The extra time (in seconds) for a item to be served from cache while it's retrieval (after ttl expiration) is in progress; Defaults to 0 (zero)
  • concurrentFetches - The number of concurrent item retrieval calls for the same cache key, after which lookup() calls will be queued - in case the item is not found in cache; Example: If your fetch function will make an HTTP request to a backend service, this will be the number of concurrent requests (per cache key) hitting the backend service; Defaults to 1 (zero)
  • holdMax - The maximum number of queued lookup() calls, after which calls will start to be answered with an error; Defaults to Infinity
  • firstFetchServesAll - If set to true and concurrentFetches is higher than 1, this means that when the first on-going fetch finishes, all the pending calls (queued and on-going) will be fulfilled; If set to false all on-going fetches will only fulfill their own lookup() calls; Defaults to true
  • errorFailsAll - If set to true, if a fetch function returns an error, it will fulfill all queued lookup() calls with the returned errors; If set to false, a fetch function returning an error will only result in an error being returned to its corresponding lookup() call; Defaults to false
  • cleanupInterval - The interval for cleaning up "dead" items in the cache. Dead items are expired items after their grace period.