cachegoose
Advanced tools
Comparing version 3.0.2 to 3.1.2
@@ -22,3 +22,3 @@ let generateKey = require('./generate-key') | ||
Aggregate.prototype.exec = function(callback) { | ||
Aggregate.prototype.exec = function(callback = function() { }) { | ||
if (!this.hasOwnProperty('_ttl')) return exec.apply(this, arguments); | ||
@@ -25,0 +25,0 @@ |
@@ -18,2 +18,3 @@ let generateKey = require('./generate-key'); | ||
, ttl = this._ttl | ||
, isCount = this.op === 'count' | ||
, isLean = this._mongooseOptions.lean | ||
@@ -27,2 +28,9 @@ , model = this.model.modelName | ||
if (cachedResults) { | ||
if (isCount) { | ||
if (debug) cachedResults = { count: cachedResults, _fromCache: true }; | ||
callback(null, cachedResults); | ||
return resolve(cachedResults); | ||
} | ||
if (!isLean) { | ||
@@ -29,0 +37,0 @@ let constructor = mongoose.model(model); |
@@ -23,5 +23,7 @@ 'use strict'; | ||
Aggregate.prototype.exec = function (callback) { | ||
Aggregate.prototype.exec = function () { | ||
var _this = this; | ||
var callback = arguments.length <= 0 || arguments[0] === undefined ? function () {} : arguments[0]; | ||
if (!this.hasOwnProperty('_ttl')) return exec.apply(this, arguments); | ||
@@ -28,0 +30,0 @@ |
@@ -24,2 +24,3 @@ 'use strict'; | ||
ttl = this._ttl, | ||
isCount = this.op === 'count', | ||
isLean = this._mongooseOptions.lean, | ||
@@ -32,2 +33,9 @@ model = this.model.modelName, | ||
if (cachedResults) { | ||
if (isCount) { | ||
if (debug) cachedResults = { count: cachedResults, _fromCache: true }; | ||
callback(null, cachedResults); | ||
return resolve(cachedResults); | ||
} | ||
if (!isLean) { | ||
@@ -34,0 +42,0 @@ var _constructor = mongoose.model(model); |
{ | ||
"name": "cachegoose", | ||
"version": "3.0.2", | ||
"version": "3.1.2", | ||
"description": "Mongoose caching that actually works.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -255,6 +255,6 @@ /* jshint expr: true, unused: false */ | ||
it('should cache aggregate queries', function(done) { | ||
aggregate(60, function(err, res) { | ||
it('should cache aggregate queries', function (done) { | ||
aggregate(60, function (err, res) { | ||
Boolean(res._fromCache).should.be.false; | ||
aggregate(60, function(err, res2) { | ||
aggregate(60, function (err, res2) { | ||
Boolean(res2._fromCache).should.be.true; | ||
@@ -266,2 +266,17 @@ done(); | ||
it('should cache aggregate queries that use Promises', function (done) { | ||
aggregate(60) | ||
.then((res) => { | ||
Boolean(res._fromCache).should.be.false; | ||
}) | ||
.then(() => { | ||
return aggregate(60); | ||
}) | ||
.then((res) => { | ||
Boolean(res._fromCache).should.be.true; | ||
}) | ||
.then(() => done()) | ||
.catch(done); | ||
}); | ||
it('should clear a custom cache key', function(done) { | ||
@@ -280,2 +295,13 @@ getAllCustomKey(60, 'custom-key', function(err, res) { | ||
}); | ||
it('should cache a `count` query', function(done) { | ||
Record.find({}).cache(60).count().exec(function(err, res) { | ||
Boolean(res._fromCache).should.be.false; | ||
Record.find({}).cache(60).count().exec(function(err, res) { | ||
Boolean(res._fromCache).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -282,0 +308,0 @@ |
27546
702