Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cache-manager

Package Overview
Dependencies
Maintainers
1
Versions
113
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-manager

Cache Manager for Node.js

  • 6.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9M
increased by4.09%
Maintainers
1
Weekly downloads
 
Created

What is cache-manager?

The cache-manager npm package is a flexible caching library for Node.js applications, which supports a variety of storage solutions and provides a uniform API to interact with different caching mechanisms. It allows for easy integration and switching between different cache stores without changing the underlying application code.

What are cache-manager's main functionalities?

Caching and Retrieving Data

This feature allows you to cache data in memory and retrieve it using a key. The 'set' method stores the value, and the 'get' method retrieves it. The 'ttl' option specifies the time-to-live in seconds.

{"const cacheManager = require('cache-manager');
const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10/*seconds*/ });

// Now set a value
memoryCache.set('myKey', 'myValue', { ttl: 5 }, (err) => {
  if (err) { throw err; }

  // Get the value
  memoryCache.get('myKey', (error, result) => {
    console.log(result);
    // >> 'myValue'
  });
});
}

Cache Store Agnosticism

Cache-manager supports different stores such as memory, Redis, and more. This feature allows you to switch between different cache stores seamlessly. The example shows how to use Redis as the cache store.

{"const cacheManager = require('cache-manager');
const redisStore = require('cache-manager-redis-store');
const redisCache = cacheManager.caching({ store: redisStore, host: 'localhost', port: 6379, auth_pass: 'XXXX', db: 0, ttl: 600 });

// Listen for redis ready event
redisCache.store.events.on('redisReady', () => {
  console.log('Redis is ready');
});

// Listen for redis error event
redisCache.store.events.on('redisError', (error) => {
  console.error('Redis error', error);
});
}

Multi-Level Caching

Cache-manager allows for multi-level caching, where you can have a hierarchy of cache stores. Data is first checked in the fastest cache (e.g., memory), and if not found, it falls back to slower caches (e.g., Redis).

{"const cacheManager = require('cache-manager');
const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10 });
const redisCache = cacheManager.caching({ store: require('cache-manager-redis-store'), ttl: 600 });
const multiCache = cacheManager.multiCaching([memoryCache, redisCache]);

multiCache.set('foo', 'bar', { ttl: 5 }, (err) => {
  if (err) { throw err; }

  multiCache.get('foo', (error, result) => {
    console.log(result);
    // >> 'bar'
  });
});
}

Other packages similar to cache-manager

Keywords

FAQs

Package last updated on 11 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc