You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
8.8 KiB

[![build status](https://secure.travis-ci.org/BryanDonovan/node-cache-manager.png)](http://travis-ci.org/BryanDonovan/node-cache-manager)
[![Coverage Status](https://coveralls.io/repos/BryanDonovan/node-cache-manager/badge.png?branch=master)](https://coveralls.io/r/BryanDonovan/node-cache-manager?branch=master)
12 years ago
node-cache-manager
12 years ago
======================
# 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](https://github.com/visionmedia/mocha),
12 years ago
[istanbul](https://github.com/yahoo/istanbul), and [sinon](http://sinonjs.org).
## Express.js Example
See the [Express.js cache-manager example app](https://github.com/BryanDonovan/node-cache-manager-express-example) to see how to use
``node-cache-manager`` in your applications.
12 years ago
## Installation
12 years ago
npm install cache-manager
12 years ago
9 years ago
## Store Engines
9 years ago
* [node-cache-manager-redis](https://github.com/dial-once/node-cache-manager-redis)
* [node-cache-manager-mongodb](https://github.com/v4l3r10/node-cache-manager-mongodb)
* [node-cache-manager-mongoose](https://github.com/disjunction/node-cache-manager-mongoose)
* [node-cache-manager-fs](https://github.com/hotelde/node-cache-manager-fs)
* [node-cache-manager-fs-binary](https://github.com/sheershoff/node-cache-manager-fs-binary)
* [node-cache-manager-hazelcast](https://github.com/marudor/node-cache-manager-hazelcast)
* [node-cache-manager-memcached-store](https://github.com/theogravity/node-cache-manager-memcached-store)
12 years ago
## Overview
First, it includes a `wrap` function that lets you wrap any function in cache.
(Note, this was inspired by [node-caching](https://github.com/mape/node-caching).)
11 years ago
This is probably the feature you're looking for. As an example, where you might have to do this:
11 years ago
```javascript
function getCachedUser(id, cb) {
memoryCache.get(id, function (err, result) {
11 years ago
if (err) { return cb(err); }
if (result) {
return cb(null, result);
}
getUser(id, function (err, result) {
11 years ago
if (err) { return cb(err); }
memoryCache.set(id, result);
11 years ago
cb(null, result);
});
});
}
```
... you can instead use the `wrap` function:
```javascript
function getCachedUser(id, cb) {
memoryCache.wrap(id, function (cacheCallback) {
getUser(id, cacheCallback);
10 years ago
}, {ttl: ttl}, cb);
11 years ago
}
```
Second, node-cache-manager features a built-in memory cache (using [node-lru-cache](https://github.com/isaacs/node-lru-cache)),
with the standard functions you'd expect in most caches:
12 years ago
9 years ago
set(key, val, {ttl: ttl}, cb) // * see note below
12 years ago
get(key, cb)
del(key, cb)
9 years ago
// * Note that depending on the underlying store, you may be able to pass the
// ttl as the third param, like this:
set(key, val, ttl, cb)
// ... or pass no ttl at all:
set(key, val, cb)
12 years ago
Third, node-cache-manager lets you set up a tiered cache strategy. This may be of
12 years ago
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,
12 years ago
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
12 years ago
node-cache-manager handles easily and transparently.
12 years ago
## 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.
12 years ago
### Single Store
12 years ago
```javascript
var cacheManager = require('cache-manager');
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
var ttl = 5;
11 years ago
// Note: callback is optional in set() and del().
memoryCache.set('foo', 'bar', {ttl: ttl}, function(err) {
11 years ago
if (err) { throw err; }
memoryCache.get('foo', function(err, result) {
11 years ago
console.log(result);
// >> 'bar'
memoryCache.del('foo', function(err) {});
11 years ago
});
});
function getUser(id, cb) {
11 years ago
setTimeout(function () {
console.log("Returning user from slow database.");
cb(null, {id: id, name: 'Bob'});
}, 100);
}
var userId = 123;
var key = 'user_' + userId;
11 years ago
// Note: ttl is optional in wrap()
memoryCache.wrap(key, function (cb) {
getUser(userId, cb);
}, {ttl: ttl}, function (err, user) {
11 years ago
console.log(user);
// Second time fetches user from memoryCache
memoryCache.wrap(key, function (cb) {
getUser(userId, cb);
11 years ago
}, function (err, user) {
console.log(user);
});
});
// Outputs:
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
```
12 years ago
#### Example Using Promises
```javascript
memoryCache.wrap(key, function() {
return getUserPromise(userId);
})
.then(function(user) {
console.log('User:', user);
});
```
If you are using a Node version that does not include native promises, you can
specify your promise dependency in the options passed to the cache module.
E.g.,
```javascript
var Promise = require('es6-promise').Promise;
cache = caching({store: store, promiseDependency: Promise});
```
9 years ago
#### Example Express App Usage
(Also see the [Express.js cache-manager example app](https://github.com/BryanDonovan/node-cache-manager-express-example)).
11 years ago
```javascript
function respond(res, err, data) {
if (err) {
11 years ago
res.json(500, err);
} else {
res.json(200, data);
}
}
app.get('/foo/bar', function(req, res) {
var cacheKey = 'foo-bar:' + JSON.stringify(req.query);
var ttl = 10;
memoryCache.wrap(cacheKey, function(cacheCallback) {
DB.find(req.query, cacheCallback);
10 years ago
}, {ttl: ttl}, function(err, result) {
11 years ago
respond(res, err, result);
});
});
```
#### Custom Stores
You can use your own custom store by creating one with the same API as the
9 years ago
built-in memory stores (such as a redis or memcached store). To use your own store just pass
in an instance of it.
E.g.,
```javascript
var myStore = require('your-homemade-store');
var cache = cacheManager.caching({store: myStore});
12 years ago
```
12 years ago
### Multi-Store
```javascript
var multiCache = cacheManager.multiCaching([memoryCache, someOtherCache]);
userId2 = 456;
key2 = 'user_' + userId;
ttl = 5;
11 years ago
// Sets in all caches.
10 years ago
multiCache.set('foo2', 'bar2', {ttl: ttl}, function(err) {
11 years ago
if (err) { throw err; }
// Fetches from highest priority cache that has the key.
multiCache.get('foo2', function(err, result) {
11 years ago
console.log(result);
// >> 'bar2'
// Delete from all caches
multiCache.del('foo2');
11 years ago
});
});
10 years ago
// Note: options with ttl are optional in wrap()
multiCache.wrap(key2, function (cb) {
getUser(userId2, cb);
10 years ago
}, {ttl: ttl}, function (err, user) {
11 years ago
console.log(user);
// Second time fetches user from memoryCache, since it's highest priority.
11 years ago
// If the data expires in the memory cache, the next fetch would pull it from
// the 'someOtherCache', and set the data in memory again.
multiCache.wrap(key2, function (cb) {
getUser(userId2, cb);
11 years ago
}, function (err, user) {
console.log(user);
});
});
```
12 years ago
### Specifying What to Cache
10 years ago
Both the `caching` and `multicaching` modules allow you to pass in a callback function named
`isCacheableValue` which is called with every value returned from cache or from a wrapped function.
This lets you specify which values should and should not be cached. If the function returns true, it will be
stored in cache. By default the caches cache everything except `undefined`.
For example, if you don't want to cache `false` and `null`, you can pass in a function like this:
```javascript
var isCacheableValue = function(value) {
return value !== null && value !== false && value !== undefined;
};
```
Then pass it to `caching` like this:
```javascript
var memoryCache = cacheManager.caching({store: 'memory', isCacheableValue: isCacheableValue};
```
And pass it to `multicaching` like this:
```javascript
var multiCache = cacheManager.multiCaching([memoryCache, someOtherCache], {
isCacheableValue: isCacheableValue
});
```
## Docs
To generate JSDOC 3 documentation:
make docs
12 years ago
## 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
10 years ago
for any new features or bug fixes. Also run `make` before submitting the pull request.
12 years ago
## License
12 years ago
node-cache-manager is licensed under the MIT license.