Git Product home page Git Product logo

node-cache-manager's Introduction

node-cache-manager

codecov tests license npm npm

Flexible NodeJS cache module

A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface.

Features

  • Made with Typescript and compatible with ESModules
  • Easy way to wrap any function in cache.
  • Tiered caches -- data gets stored in each cache and fetched from the highest. priority cache(s) first.
  • Use any cache you want, as long as it has the same API.
  • 100% test coverage via vitest.

Installation

pnpm install cache-manager

Usage Examples

Single Store

import { caching } from 'cache-manager';

const memoryCache = await caching('memory', {
  max: 100,
  ttl: 10 * 1000 /*milliseconds*/,
});

const ttl = 5 * 1000; /*milliseconds*/
await memoryCache.set('foo', 'bar', ttl);

console.log(await memoryCache.get('foo'));
// >> "bar"

await memoryCache.del('foo');

console.log(await memoryCache.get('foo'));
// >> undefined

const getUser = (id: string) => new Promise.resolve({ id: id, name: 'Bob' });

const userId = 123;
const key = 'user_' + userId;

console.log(await memoryCache.wrap(key, () => getUser(userId), ttl));
// >> { id: 123, name: 'Bob' }

See unit tests in test/caching.test.ts for more information.

Example setting/getting several keys with mset() and mget()

await memoryCache.store.mset(
  [
    ['foo', 'bar'],
    ['foo2', 'bar2'],
  ],
  ttl,
);

console.log(await memoryCache.store.mget('foo', 'foo2'));
// >> ['bar', 'bar2']

// Delete keys with mdel() passing arguments...
await memoryCache.store.mdel('foo', 'foo2');

Custom Stores

You can use your own custom store by creating one with the same API as the built-in memory stores.

Create single cache store synchronously

As caching() requires async functionality to resolve some stores, this is not well-suited to use for default function/constructor parameters etc.

If you need to create a cache store synchronously, you can instead use createCache():

import { createCache, memoryStore } from 'node-cache-manager';

// Create memory cache synchronously
const memoryCache = createCache(memoryStore(), {
  max: 100,
  ttl: 10 * 1000 /*milliseconds*/,
});

// Default parameter in function
function myService(cache = createCache(memoryStore())) {}

// Default parameter in class constructor
const DEFAULT_CACHE = createCache(memoryStore(), { ttl: 60 * 1000 });
// ...
class MyService {
  constructor(private cache = DEFAULT_CACHE) {}
}

Multi-Store

import { multiCaching } from 'cache-manager';

const multiCache = multiCaching([memoryCache, someOtherCache]);
const userId2 = 456;
const key2 = 'user_' + userId;
const ttl = 5;

// Sets in all caches.
await multiCache.set('foo2', 'bar2', ttl);

// Fetches from highest priority cache that has the key.
console.log(await multiCache.get('foo2'));
// >> "bar2"

// Delete from all caches
await multiCache.del('foo2');

// Sets multiple keys in all caches.
// You can pass as many key, value tuples as you want
await multiCache.mset(
  [
    ['foo', 'bar'],
    ['foo2', 'bar2'],
  ],
  ttl
);

// mget() fetches from highest priority cache.
// If the first cache does not return all the keys,
// the next cache is fetched with the keys that were not found.
// This is done recursively until either:
// - all have been found
// - all caches has been fetched
console.log(await multiCache.mget('key', 'key2'));
// >> ['bar', 'bar2']

// Delete keys with mdel() passing arguments...
await multiCache.mdel('foo', 'foo2');

See unit tests in test/multi-caching.test.ts for more information.

Cache Manager Options

The caching and multiCaching functions accept an options object as the second parameter. The following options are available:

  • max: The maximum number of items that can be stored in the cache. If the cache is full, the least recently used item is removed.
  • ttl: The time to live in milliseconds. This is the maximum amount of time that an item can be in the cache before it is removed.
  • shouldCloneBeforeSet: If true, the value will be cloned before being set in the cache. This is set to true by default.
import { caching } from 'cache-manager';

const memoryCache = await caching('memory', {
  max: 100,
  ttl: 10 * 1000 /*milliseconds*/,
  shouldCloneBeforeSet: false, // this is set true by default (optional)
});

Refresh cache keys in background

Both the caching and multicaching modules support a mechanism to refresh expiring cache keys in background when using the wrap function.
This is done by adding a refreshThreshold attribute while creating the caching store or passing it to the wrap function.

If refreshThreshold is set and after retrieving a value from cache the TTL will be checked.
If the remaining TTL is less than refreshThreshold, the system will update the value asynchronously,
following same rules as standard fetching. In the meantime, the system will return the old value until expiration.

NOTES:

  • In case of multicaching, the store that will be checked for refresh is the one where the key will be found first (highest priority).
  • If the threshold is low and the worker function is slow, the key may expire and you may encounter a racing condition with updating values.
  • The background refresh mechanism currently does not support providing multiple keys to wrap function.
  • If no ttl is set for the key, the refresh mechanism will not be triggered. For redis, the ttl is set to -1 by default.

For example, pass the refreshThreshold to caching like this:

const memoryCache = await caching('memory', {
  max: 100,
  ttl: 10 * 1000 /*milliseconds*/,
  refreshThreshold: 3 * 1000 /*milliseconds*/,
  
  /* optional, but if not set, background refresh error will be an unhandled
   * promise rejection, which might crash your node process */
  onBackgroundRefreshError: (error) => { /* log or otherwise handle error */ }
});

When a value will be retrieved from Redis with a TTL minor than 3sec, the value will be updated in the background.

Store Engines

Official and updated to last version

Third party

Contribute

If you would like to contribute to the project, please read how to contribute here CONTRIBUTING.md.

License

cache-manager is licensed under the MIT license.

node-cache-manager's People

Contributors

bryandonovan avatar renovate[bot] avatar zzau13 avatar jaredwray avatar marcoreni avatar seanzx85 avatar aletorrado avatar philippeauriach avatar gswalden avatar sebelga avatar anchan828 avatar pukoren avatar elliotttf avatar ricardomozartlino avatar lchenay avatar v4l3r10 avatar dependabot[bot] avatar raadad avatar orgads avatar mojtaba-na avatar dabroek avatar tmbobbins avatar lukechilds avatar imjohnbo avatar nicolasmahe avatar r3volut1oner avatar quentinlemcode avatar ricall avatar deyhle avatar tirke 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.