What is @types/cache-manager?
@types/cache-manager provides TypeScript type definitions for the cache-manager library, which is a multi-level caching library that supports various storage engines.
What are @types/cache-manager's main functionalities?
Basic In-Memory Caching
This feature allows you to set up a basic in-memory cache with a specified time-to-live (TTL) and maximum size.
const cacheManager = require('cache-manager');
const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10 /* seconds */ });
memoryCache.set('foo', 'bar', { ttl: 10 }, (err) => {
if (err) throw err;
memoryCache.get('foo', (err, result) => {
if (err) throw err;
console.log(result); // 'bar'
});
});
Multi-Level Caching
This feature allows you to set up multi-level caching, combining different storage engines like in-memory and Redis.
const cacheManager = require('cache-manager');
const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10 });
const redisStore = require('cache-manager-redis-store');
const redisCache = cacheManager.caching({ store: redisStore, host: 'localhost', port: 6379, ttl: 600 });
const multiCache = cacheManager.multiCaching([memoryCache, redisCache]);
multiCache.set('foo', 'bar', { ttl: 10 }, (err) => {
if (err) throw err;
multiCache.get('foo', (err, result) => {
if (err) throw err;
console.log(result); // 'bar'
});
});
Custom Store
This feature allows you to create a custom store by defining your own get, set, and delete methods.
const cacheManager = require('cache-manager');
const customStore = {
get: (key, callback) => {
// Custom get logic
callback(null, 'value');
},
set: (key, value, options, callback) => {
// Custom set logic
callback(null);
},
del: (key, callback) => {
// Custom delete logic
callback(null);
}
};
const customCache = cacheManager.caching({ store: customStore });
customCache.set('foo', 'bar', { ttl: 10 }, (err) => {
if (err) throw err;
customCache.get('foo', (err, result) => {
if (err) throw err;
console.log(result); // 'bar'
});
});
Other packages similar to @types/cache-manager
node-cache
node-cache is a simple in-memory caching module for Node.js. It is less complex than cache-manager and does not support multi-level caching or multiple storage engines.
redis
The redis package is a client for Redis, a powerful in-memory data structure store. While it provides caching capabilities, it is more complex and feature-rich compared to cache-manager, which abstracts multiple storage engines.
lru-cache
lru-cache is a simple Least Recently Used (LRU) cache for Node.js. It is focused on in-memory caching and does not support multiple storage engines or multi-level caching like cache-manager.