diff --git a/README.md b/README.md index 0b4d8e1..93f21e4 100644 --- a/README.md +++ b/README.md @@ -28,4 +28,15 @@ For enabling this cache in your query just call .cache() db.User.find({ ... }).cache().exec(function() { ... }) ``` +To invalidate the cache for a query, just call invalidateCache() +```javascript +db.User.find({ ... }).invalidateCache().exec(function() { ... }) +``` +This will clear the current query from the cache, other queries may remain +in the cache, nevertheless. If you want to clear/reset the whole cache, run +invalidateCache(true) on Query +```javascript +db.User.find({ ... }).invalidateCache(true).exec(function() { ... }) +``` + For more talky output add ```debug: true``` to the cacheOpts. diff --git a/lib/cache.js b/lib/cache.js index 35477ff..0b6c5d0 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -14,6 +14,21 @@ exports.install = module.exports.install = function(mongoose, options, Aggregate return this; }; + mongoose.Query.prototype.invalidateCache = function(resetGlobal) { + var key = genKey(this); + + if (resetGlobal) { + cache.reset(); + return this; + } + if (!cache.has(key)) { + log('Requested cache invalidation, but this isn\'t cached.'); + return this; + } + cache.del(key); + return this; + }; + if (typeof Aggregate !== 'undefined') { orig.execAggregate = Aggregate.prototype.exec; Aggregate.prototype.cache = function() {