From d26bc6ede024c428a274b068d0b4775785594c2a Mon Sep 17 00:00:00 2001 From: tenorok Date: Sat, 13 Mar 2021 22:16:55 +0300 Subject: [PATCH] Support null ttl to disable caching. --- README.md | 7 +++++++ src/cache.js | 1 + src/extend-aggregate.js | 2 +- src/extend-query.js | 2 +- test/index.js | 20 +++++++++++++++++++- 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ea76a57..e7eb6e4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/cache.js b/src/cache.js index d04a6c2..9301ee3 100644 --- a/src/cache.js +++ b/src/cache.js @@ -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); }; diff --git a/src/extend-aggregate.js b/src/extend-aggregate.js index b7d4f12..7406bee 100644 --- a/src/extend-aggregate.js +++ b/src/extend-aggregate.js @@ -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); } diff --git a/src/extend-query.js b/src/extend-query.js index 8089ffb..0be082b 100644 --- a/src/extend-query.js +++ b/src/extend-query.js @@ -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); diff --git a/test/index.js b/test/index.js index 215d822..236ee66 100644 --- a/test/index.js +++ b/test/index.js @@ -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); @@ -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);