Skip to content

Commit

Permalink
Add invalidateCache() method for cache invalidation
Browse files Browse the repository at this point in the history
Fixes Gottox#17
  • Loading branch information
FlorianSW committed May 1, 2017
1 parent c31537c commit f99cc26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 15 additions & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit f99cc26

Please sign in to comment.