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 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-cache-plus

v2.1.0

Published

A wrapper around the popular node-cache library, featuring tag-based invalidation, factory functions, and more.

Readme

node-cache-plus

node-cache-plus is a wrapper around the popular library node-cache with additional features such as tag-based invalidation, factory functions, and other helpers for in-memory caching in Node.js applications.

Sorry 🦖, this is an ESM-only package.

Installation

To install node-cache-plus, use your preferred package manager:

npm install node-cache-plus

Usage

node-cache-plus is a drop-in replacement for node-cache. It provides the same API as node-cache with additional features. Refer to the node-cache documentation for more information on the basic usage and initialisation options.

TypeDoc reference is available here.

Basic Usage

import { Cache } from "node-cache-plus";

const cache = new Cache();

// Set a value with a TTL of 60 seconds
cache.set("key", "value", 60);

// Get the value
const value = cache.get("key");
console.log(value); // Output: "value"

// Delete the value
cache.del("key");

Tag-based Invalidation

Invalidate a single tag

invalidateTag method invalidates all keys with a specific tag.

// Set values with tags
cache.set("key1", "value1", 60, ["tag1"]);
cache.set("key2", "value2", 60, ["tag2"]);
cache.set("key3", "value3", 60, ["tag1", "tag2"]);

// Invalidate all keys with a specific tag
cache.invalidateTag("tag1"); // i.e. Invalidates keys "key1" and "key3"

cache.get("key1"); // Output: undefined
cache.get("key2"); // Output: "value2"
cache.get("key3"); // Output: undefined

Invalidate Intersection of Tags

invalidateTagsIntersection method invalidates keys that have all specified tags.

// Set values with tags
cache.set("key1", "value1", 60, ["tag1"]);
cache.set("key2", "value2", 60, ["tag2"]);
cache.set("key3", "value3", 60, ["tag1", "tag2"]);

// Invalidate keys that have all specified tags
cache.invalidateTagsIntersection(["tag1", "tag2"]); // i.e. Invalidates key "key3"

cache.get("key1"); // Output: "value1"
cache.get("key2"); // Output: "value2"
cache.get("key3"); // Output: undefined

Invalidate Union of Tags

invalidateTagsUnion method invalidates keys that have at least one of the specified tags.

// Set values with tags
cache.set("key1", "value1", 60, ["tag1"]);
cache.set("key2", "value2", 60, ["tag2"]);
cache.set("key3", "value3", 60, ["tag1", "tag2"]);

// Invalidate keys that have at least one of the specified tags
cache.invalidateTagsUnion(["tag1", "tag2"]); // i.e. Invalidates all keys

cache.get("key1"); // Output: undefined
cache.get("key2"); // Output: undefined
cache.get("key3"); // Output: undefined

withCache Helper Function

import { withCache } from "node-cache-plus";

async function fetchData(param: string): Promise<string> {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(`data_${param}`);
    }, 500);
  });
}

const cachedFetchData = withCache(fetchData, { ttl: 600, tags: ["data"] });

const data = await cachedFetchData("param1");
console.log(data); // Output: "data_param1"

Note: This function uses the default cache, see details in the Configuring Default Cache section below. You can also pass a custom cache instance as a prop to the withCache helper

cachedCall Helper Function

import { cachedCall } from "node-cache-plus";

async function fetchData(param: string): Promise<string> {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(`data_${param}`);
    }, 500);
  });
}

const data = await cachedCall(
  fetchData,
  { ttl: 600, tags: ["data"] },
  "param1"
);
console.log(data); // Output: "data_param1"

Note: This function uses the default cache, see details in the Configuring Default Cache section below. You can also pass a custom cache instance as a prop to the cachedCall helper

Configuring Default Cache

The helper functions withCache and cachedCall use a default cache instance. You can configure the default cache instance by using theconfigureDefaultCache function.

import { configureDefaultCache, getDefaultCache } from "node-cache-plus";

// Configure the default cache with custom options
configureDefaultCache({ stdTTL: 100, checkperiod: 120 });

// Get the default cache instance
const defaultCache = getDefaultCache();

// Use the default cache instance
defaultCache.set("key", "value", 60);
const value = defaultCache.get("key");
console.log(value); // Output: "value"