mongoose cachebox

Caching mongoose queries easier with cacheman that supports in-memory, and Redis engines.
Instalation
$ npm install mongoose-cachebox
Usage
var mongoose = require('mongoose');
var options = {
cache: true,
ttl: 30
};
mongooseCachebox(mongoose, options);
Then later any find query will be cached for 60 seconds.
You can also enable caching programatically by using the cache method directly from the query instance:
var Person = mongoose.model('Person');
Person.find({ active: true })
.cache('50s')
.exec(function (err, docs) {
if (err) throw error;
console.log(docs.ttl)
console.log(docs.stored);
console.log(docs);
});
API
This plugin will add two more methods to a mongoose query instance cache and ttl.
query.cache([cached], [ttl])
Both parameters cache and ttl are optional, the first one is for enable caching the second is for specifying the cache expiration (time to live).
For start caching just call the cache method:
Person.find({ active: true })
.cache()
.exec(function (err, docs) {
});
The above is equivalent to this:
Person.find({ active: true })
.cache(true)
.exec(function (err, docs) {
});
You can specify the ttl (time to live) value directly:
Person.find({ active: true })
.cache(10)
.exec(function (err, docs) {
});
The above is equivalent to this:
Person.find({ active: true })
.cache(true, 10)
.exec(function (err, docs) {
});
And to disable caching for specific query just pass false:
Person.find({ active: true })
.cache(false)
.exec(function (err, docs) {
});
query.ttl(ttl)
By default the ttl value is 60000 (60 seconds) but you can use the ttl method to specify a different value:
Person.find({ active: true })
.cache()
.ttl(10)
.exec(function (err, docs) {
});
Redis
By default mongoose-cachebox will use the memory engine to cache queries but it can cache queries using Redis by specifying redis engine when initializing the plugin:
var mongoose = require('mongoose');
var options = {
engine: 'redis',
host: '127.0.0.1',
port: '6379',
password: 'secret'
};
mongooseCachebox(mongoose, options);
This module use cacheman for the caching magic, so check out the project for more details and options.
Run tests
$ make test
License
(The MIT License)
Copyright (c) 2013 Jonathan Brumley <cayasso@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.