cachegoose
Advanced tools
Comparing version
@@ -14,2 +14,3 @@ let Cacheman = require('cacheman') | ||
Cache.prototype.set = function(key, value, ttl, cb = noop) { | ||
if (ttl === 0) ttl = -1; | ||
return this._cache.set(key, value, ttl, cb); | ||
@@ -16,0 +17,0 @@ }; |
@@ -21,3 +21,3 @@ let hasBeenExtended = false; | ||
Aggregate.prototype.exec = function(callback) { | ||
let key = this.getCacheKey() | ||
let key = this._key || this.getCacheKey() | ||
, ttl = this._ttl | ||
@@ -46,9 +46,10 @@ , promise = new mongoose.Promise() | ||
Aggregate.prototype.cache = function(ttl) { | ||
if (ttl === false) { | ||
cache.del(this.getCacheKey()); | ||
} else { | ||
this._ttl = ttl || 60; | ||
Aggregate.prototype.cache = function(ttl = 60, customKey = '') { | ||
if (typeof ttl === 'string') { | ||
customKey = ttl; | ||
ttl = 60; | ||
} | ||
this._ttl = ttl; | ||
this._key = customKey; | ||
return this; | ||
@@ -55,0 +56,0 @@ }; |
@@ -14,3 +14,3 @@ module.exports = function(mongoose, cache) { | ||
let key = this.getCacheKey() | ||
let key = this._key || this.getCacheKey() | ||
, ttl = this._ttl | ||
@@ -48,9 +48,10 @@ , isLean = this._mongooseOptions.lean | ||
mongoose.Query.prototype.cache = function(ttl = 60) { | ||
if (ttl === false) { | ||
cache.del(this.getCacheKey()); | ||
} else { | ||
this._ttl = ttl; | ||
mongoose.Query.prototype.cache = function(ttl = 60, customKey = '') { | ||
if (typeof ttl === 'string') { | ||
customKey = ttl; | ||
ttl = 60; | ||
} | ||
this._ttl = ttl; | ||
this._key = customKey; | ||
return this; | ||
@@ -57,0 +58,0 @@ }; |
@@ -15,1 +15,6 @@ let hasRun = false | ||
}; | ||
module.exports.clearCache = function(customKey, cb = function() { }) { | ||
if (!customKey) return cb(); | ||
cache.del(customKey, cb); | ||
}; |
@@ -22,2 +22,3 @@ "use strict"; | ||
return (function () { | ||
if (ttl === 0) ttl = -1; | ||
return _this2._cache.set(key, value, ttl, cb); | ||
@@ -24,0 +25,0 @@ })(); |
@@ -24,3 +24,3 @@ "use strict"; | ||
var _this = this; | ||
var key = this.getCacheKey(), | ||
var key = this._key || this.getCacheKey(), | ||
ttl = this._ttl, | ||
@@ -48,9 +48,12 @@ promise = new mongoose.Promise(); | ||
Aggregate.prototype.cache = function (ttl) { | ||
if (ttl === false) { | ||
cache.del(this.getCacheKey()); | ||
} else { | ||
this._ttl = ttl || 60; | ||
Aggregate.prototype.cache = function () { | ||
var ttl = arguments[0] === undefined ? 60 : arguments[0]; | ||
var customKey = arguments[1] === undefined ? "" : arguments[1]; | ||
if (typeof ttl === "string") { | ||
customKey = ttl; | ||
ttl = 60; | ||
} | ||
this._ttl = ttl; | ||
this._key = customKey; | ||
return this; | ||
@@ -57,0 +60,0 @@ }; |
@@ -18,3 +18,3 @@ "use strict"; | ||
var key = this.getCacheKey(), | ||
var key = this._key || this.getCacheKey(), | ||
ttl = this._ttl, | ||
@@ -51,8 +51,10 @@ isLean = this._mongooseOptions.lean, | ||
var ttl = arguments[0] === undefined ? 60 : arguments[0]; | ||
if (ttl === false) { | ||
cache.del(this.getCacheKey()); | ||
} else { | ||
this._ttl = ttl; | ||
var customKey = arguments[1] === undefined ? "" : arguments[1]; | ||
if (typeof ttl === "string") { | ||
customKey = ttl; | ||
ttl = 60; | ||
} | ||
this._ttl = ttl; | ||
this._key = customKey; | ||
return this; | ||
@@ -59,0 +61,0 @@ }; |
@@ -15,2 +15,8 @@ "use strict"; | ||
require("./extend-aggregate")(mongoose, cache); | ||
}; | ||
module.exports.clearCache = function (customKey) { | ||
var cb = arguments[1] === undefined ? function () {} : arguments[1]; | ||
if (!customKey) return cb(); | ||
cache.del(customKey, cb); | ||
}; |
{ | ||
"name": "cachegoose", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Mongoose caching that actually works.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -33,3 +33,3 @@ #cachegoose | ||
.group({ total: { $sum: '$some_field' } }) | ||
.cache(30) // The number of seconds to cache the query. Defaults to 60 seconds. | ||
.cache(0) // Explicitly passing in 0 will cache the results indefinitely. | ||
.exec(function(err, aggResults) { | ||
@@ -40,2 +40,29 @@ ... | ||
You can also pass a custom key into the `.cache()` method, which you can then use later to clear the cached content. | ||
```javascript | ||
var mongoose = require('mongoose'); | ||
var cachegoose = require('cachegoose'); | ||
cachegoose(mongoose, { | ||
engine: 'redis', /* If you don't specify the redis engine, */ | ||
port: 6379, /* the query results will be cached in memory. */ | ||
host: 'localhost' | ||
}); | ||
var userId = '1234567890'; | ||
Children | ||
.find({ parentId: userId }) | ||
.cache(0, userId + '-children') /* Will create a redis entry */ | ||
.exec(function(err, records) { /* with the key '1234567890-children' */ | ||
... | ||
}); | ||
ChildrenSchema.post('save', function(child) { | ||
// Clear the parent's cache, since a new child has been added. | ||
cachegoose.clearCache(child.parentId + '-children'); | ||
}); | ||
``` | ||
That's pretty much it. Just insert `.cache()` into the queries you want to cache, and they will be cached. Works with `select`, `lean`, `sort`, and anything else that will modify the results of a query. | ||
@@ -42,0 +69,0 @@ |
@@ -238,2 +238,16 @@ /* jshint expr: true, unused: false */ | ||
}); | ||
it('should clear a custom cache key', function(done) { | ||
getAllCustomKey(60, 'custom-key', function(err, res) { | ||
Boolean(res._fromCache).should.be.false; | ||
getAllCustomKey(60, 'custom-key', function(err, res2) { | ||
Boolean(res2._fromCache).should.be.true; | ||
cachegoose.clearCache('custom-key'); | ||
getAllCustomKey(60, 'custom-key', function(err, res3) { | ||
Boolean(res3._fromCache).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -245,2 +259,6 @@ | ||
function getAllCustomKey(ttl, key, cb) { | ||
return Record.find({}).cache(ttl, key).exec(cb); | ||
} | ||
function getAllNoCache(cb) { | ||
@@ -247,0 +265,0 @@ return Record.find({}).exec(cb); |
22764
10.36%589
6.13%70
62.79%