Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add invalidateCache() method for cache invalidation #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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