cache-manager
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -0,1 +1,4 @@ | ||
- 2.4.0 2017-01-17 | ||
- Added ability to use a dynamic cache ttl (#65) - @philippeauriach | ||
- 2.3.0 2016-12-22 | ||
@@ -2,0 +5,0 @@ - Updating isCacheableValue description in README; README syntax error fix (#70, #71) - @lukechilds |
@@ -116,2 +116,6 @@ /** @module cacheManager/caching */ | ||
if (options && typeof options.ttl === 'function') { | ||
options.ttl = options.ttl(data); | ||
} | ||
self.store.set(key, data, options, function(err) { | ||
@@ -118,0 +122,0 @@ if (err && (!self.ignoreCacheErrors)) { |
{ | ||
"name": "cache-manager", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -155,2 +155,22 @@ [![build status](https://secure.travis-ci.org/BryanDonovan/node-cache-manager.svg)](http://travis-ci.org/BryanDonovan/node-cache-manager) | ||
The `ttl` can also be computed dynamicall by passing in a function. E.g., | ||
```javascript | ||
var opts = { | ||
ttl: function(user) { | ||
if (user.id === 1) { | ||
return 0.1; | ||
} else { | ||
return 0.5; | ||
} | ||
} | ||
}; | ||
memoryCache.wrap(key, function(cb) { | ||
getUser(userId, cb); | ||
}, opts, function(err, user) { | ||
console.log(user); | ||
} | ||
``` | ||
#### Example Using Promises | ||
@@ -157,0 +177,0 @@ |
@@ -363,2 +363,29 @@ // TODO: These are really a mix of unit and integration tests. | ||
context("ttl function", function() { | ||
beforeEach(function() { | ||
sinon.spy(memoryStoreStub, 'set'); | ||
}); | ||
afterEach(function() { | ||
memoryStoreStub.set.restore(); | ||
}); | ||
it("when a ttl function is passed in", function(done) { | ||
opts = { | ||
ttl: function(widget) { | ||
assert.deepEqual(widget, {name: name}); | ||
return 0.2; | ||
} | ||
}; | ||
cache.wrap(key, function(cb) { | ||
methods.getWidget(name, cb); | ||
}, opts, function(err, widget) { | ||
checkErr(err); | ||
assert.deepEqual(widget, {name: name}); | ||
sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, {ttl: 0.2}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
context("when result is already cached", function() { | ||
@@ -365,0 +392,0 @@ function getCachedWidget(name, cb) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
137924
2916
336