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
node-cache
node-cache is an in-memory caching package similar to cache-manager's memory store. It offers a simple and fast caching solution but does not support multiple backends or a tiered caching system.
lru-cache
lru-cache is an in-memory cache that implements the LRU (Least Recently Used) eviction policy. Unlike cache-manager, it is specifically tailored for LRU caching and does not support multiple storage backends.
keyv
keyv is a simple key-value storage with support for multiple backends, including Redis, MongoDB, SQLite, and more. It provides a unified interface across different stores but does not have built-in support for multi-level caching.
node-cache-manager
Flexible NodeJS cache module
A cache module for nodejs that allows easy wrapping of functions in cache,
tiered caches, and a consistent interface.
Features
- 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 mocha,
istanbul, and sinon.
Express.js Example
See the Express.js cache-manager example app to see how to use
node-cache-manager
in your applications.
Installation
npm install cache-manager
Overview
First, it includes a wrap
function that lets you wrap any function in cache.
(Note, this was inspired by node-caching.)
This is probably the feature you're looking for. As an example, where you might have to do this:
function get_cached_user(id, cb) {
memory_cache.get(id, function (err, result) {
if (err) { return cb(err); }
if (result) {
return cb(null, result);
}
get_user(id, function (err, result) {
if (err) { return cb(err); }
memory_cache.set(id, result);
cb(null, result);
});
});
}
... you can instead use the wrap
function:
function get_cached_user(id, cb) {
memory_cache.wrap(id, function (cache_callback) {
get_user(id, cache_callback);
}, cb);
}
Second, node-cache-manager features a built-in memory cache (using node-lru-cache),
with the standard functions you'd expect in most caches:
set(key, val, cb)
get(key, cb)
del(key, cb)
Third, node-cache-manager lets you set up a tiered cache strategy. This may be of
limited use in most cases, but imagine a scenario where you expect tons of
traffic, and don't want to hit your primary cache (like Redis) for every request.
You decide to store the most commonly-requested data in an in-memory cache,
perhaps with a very short timeout and/or a small data size limit. But you
still want to store the data in Redis for backup, and for the requests that
aren't as common as the ones you want to store in memory. This is something
node-cache-manager handles easily and transparently.
Usage Examples
See examples below and in the examples directory. See examples/redis_example
for an example of how to implement a
Redis cache store with connection pooling.
Single Store
var cache_manager = require('cache-manager');
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10});
memory_cache.set('foo', 'bar', function(err) {
if (err) { throw err; }
memory_cache.get('foo', function(err, result) {
console.log(result);
memory_cache.del('foo', function(err) {});
});
});
function get_user(id, cb) {
setTimeout(function () {
console.log("Returning user from slow database.");
cb(null, {id: id, name: 'Bob'});
}, 100);
}
var user_id = 123;
var key = 'user_' + user_id;
memory_cache.wrap(key, function (cb) {
get_user(user_id, cb);
}, function (err, user) {
console.log(user);
memory_cache.wrap(key, function (cb) {
get_user(user_id, cb);
}, function (err, user) {
console.log(user);
});
});
Here's a very basic example of how you could use this in an Express app:
function respond(res, err, data) {
if (err) {
res.json(500, err);
} else {
res.json(200, data);
}
}
app.get('/foo/bar', function(req, res) {
var cache_key = 'foo-bar:' + JSON.stringify(req.query);
memory_cache.wrap(cache_key, function(cache_cb) {
DB.find(req.query, cache_cb);
}, function(err, result) {
respond(res, err, result);
});
});
Custom Stores
You can use your own custom store by creating one with the same API as the
build-in memory stores (such as a redis or memcached store). To use your own store, you can either pass
in an instance of it, or pass in the path to the module.
E.g.,
var my_store = require('your-homemade-store');
var cache = cache_manager.caching({store: my_store});
var cache = cache_manager.caching({store: '/path/to/your/store'});
Multi-Store
var multi_cache = cache_manager.multi_caching([memory_cache, some_other_cache]);
user_id2 = 456;
key2 = 'user_' + user_id;
multi_cache.set('foo2', 'bar2', function(err) {
if (err) { throw err; }
multi_cache.get('foo2', function(err, result) {
console.log(result);
multi_cache.del('foo2');
});
});
multi_cache.wrap(key2, function (cb) {
get_user(user_id2, cb);
}, function (err, user) {
console.log(user);
multi_cache.wrap(key2, function (cb) {
get_user(user_id2, cb);
}, function (err, user) {
console.log(user);
});
});
Tests
To run tests, first run:
npm install -d
Run the tests and JShint:
make
Contribute
If you would like to contribute to the project, please fork it and send us a pull request. Please add tests
for any new features or bug fixes. Also run make
before submitting the pull request.
License
node-cache-manager is licensed under the MIT license.