Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ Record
.exec(function(err, aggResults) {
...
});

Record
.find({})
.cache(null) // Explicitly passing in null will not cache the results.
.exec(function(err, records) {
...
});
```

You can also pass a custom key into the `.cache()` method, which you can then use later to clear the cached content.
Expand Down
1 change: 1 addition & 0 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Cache.prototype.get = function(key, cb = noop) {
};

Cache.prototype.set = function(key, value, ttl, cb = noop) {
if (ttl === null) return cb();
if (ttl === 0) ttl = -1;
return this._cache.set(key, value, ttl, cb);
};
Expand Down
2 changes: 1 addition & 1 deletion src/extend-aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function(mongoose, cache) {

return new Promise((resolve, reject) => {
cache.get(key, (err, cachedResults) => { //eslint-disable-line handle-callback-err
if (cachedResults) {
if (cachedResults != null && ttl !== null) {
callback(null, cachedResults);
return resolve(cachedResults);
}
Expand Down
2 changes: 1 addition & 1 deletion src/extend-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function(mongoose, cache) {

return new Promise((resolve, reject) => {
cache.get(key, (err, cachedResults) => { //eslint-disable-line handle-callback-err
if (cachedResults != null) {
if (cachedResults != null && ttl !== null) {
if (isCount) {
callback(null, cachedResults);
return resolve(cachedResults);
Expand Down
20 changes: 19 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('cachegoose', () => {
cachedRes.length.should.equal(10);
});

it('should not cache the same query w/out a ttl defined', async () => {
it('should not cache the same query w/out a cachegoose invoking', async () => {
const res = await getAll(60);
res.length.should.equal(10);

Expand All @@ -90,6 +90,24 @@ describe('cachegoose', () => {
nonCachedResponse.length.should.equal(20);
});

it('should not cache query w/out a ttl defined', async () => {
const res = await getAll(null);
res.length.should.equal(10);

await generate(10);
const cachedRes = await getAll(null);
cachedRes.length.should.equal(20);
});

it('should clear cache w/out a ttl defined', async () => {
const res = await getAll(60);
res.length.should.equal(10);

await generate(10);
const cachedRes = await getAll(null);
cachedRes.length.should.equal(20);
});

it('should return a Mongoose model from cached and non-cached results', (done) => {
getAll(60, (err, res) => {
if (err) return done(err);
Expand Down