Giter VIP home page Giter VIP logo

node-cache-hold's Introduction

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.

node-cache-hold's People

Stargazers

Enrique Saez avatar

Watchers

James Cloos avatar David Oliveira avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.