cache-manager
Advanced tools
Comparing version 3.2.0 to 3.2.1
@@ -0,1 +1,4 @@ | ||
- 3.2.1 2020-03-20 | ||
- Background refresh fixes (#139). -@marcoreni | ||
- 3.2.0 2020-03-13 | ||
@@ -2,0 +5,0 @@ - Background refresh of expiring entries (#138). - @marcoreni |
@@ -70,3 +70,3 @@ /** @module cacheManager/caching */ | ||
function handleBackgroundRefresh(key, work, options) { | ||
if (!backgroundQueue.has(key)) { | ||
if (self.refreshThreshold && !backgroundQueue.has(key)) { | ||
backgroundQueue.add(key); | ||
@@ -91,2 +91,4 @@ self.checkRefreshThreshold(key, function(err, isExpiring) { | ||
}); | ||
} else { | ||
backgroundQueue.delete(key); | ||
} | ||
@@ -93,0 +95,0 @@ }); |
@@ -327,4 +327,4 @@ /** @module cacheManager/multiCaching */ | ||
function handleBackgroundRefresh(caches, index, key, work) { | ||
if (!backgroundQueue.has(key)) { | ||
function handleBackgroundRefresh(caches, index, key, work, options) { | ||
if (caches[index].refreshThreshold && !backgroundQueue.has(key)) { | ||
backgroundQueue.add(key); | ||
@@ -342,5 +342,2 @@ caches[index].checkRefreshThreshold(key, function(err, isExpiring) { | ||
} | ||
if (options && typeof options.ttl === 'function') { | ||
options.ttl = options.ttl(workData); | ||
} | ||
var args = [caches, key, workData, options, function() { | ||
@@ -351,2 +348,4 @@ backgroundQueue.delete(key); | ||
}); | ||
} else { | ||
backgroundQueue.delete(key); | ||
} | ||
@@ -407,3 +406,3 @@ }); | ||
} else if (self._isCacheableValue(result)) { | ||
handleBackgroundRefresh(caches, index, key, work); | ||
handleBackgroundRefresh(caches, index, key, work, options); | ||
var cachesToUpdate = caches.slice(0, index); | ||
@@ -410,0 +409,0 @@ var args = [cachesToUpdate, key, result, options, function(err) { |
{ | ||
"name": "cache-manager", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1108,3 +1108,3 @@ // TODO: These are really a mix of unit and integration tests. | ||
describe("using tweaked memory (lru-cache) store with a refreshThreshold", function() { | ||
describe("using memory (lru-cache) store with a refreshThreshold", function() { | ||
var memoryStoreStub; | ||
@@ -1128,10 +1128,49 @@ var opts; | ||
context("when result is already cached", function() { | ||
function getCachedWidget(name, cb) { | ||
cache.wrap(key, function(cacheCb) { | ||
methods.getWidget(name, cacheCb); | ||
}, opts, cb); | ||
} | ||
function getCachedWidget(name, cb) { | ||
cache.wrap(key, function(cacheCb) { | ||
methods.getWidget(name, cacheCb); | ||
}, opts, cb); | ||
} | ||
context("using standard memorystore", function() { | ||
beforeEach(function(done) { | ||
getCachedWidget(name, function(err, widget) { | ||
checkErr(err); | ||
assert.ok(widget); | ||
memoryStoreStub.get(key, function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
sinon.spy(cache, 'checkRefreshThreshold'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
afterEach(function() { | ||
cache.checkRefreshThreshold.restore(); | ||
}); | ||
it("retrieves data from cache and stops background if failing while checking ttl", function(done) { | ||
// NOTE: TTL function is not defined | ||
var funcCalled = false; | ||
cache.wrap(key, function(cb) { | ||
funcCalled = true; | ||
methods.getWidget(name, function(err, result) { | ||
cb(err, result); | ||
}); | ||
}, function(err, widget) { | ||
checkErr(err); | ||
assert.deepEqual(widget, {name: name}); | ||
assert.equal(cache.checkRefreshThreshold.callCount, 1); | ||
assert.equal(funcCalled, false); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
context("using tweaked memory store, when result is already cached", function() { | ||
beforeEach(function(done) { | ||
memoryStoreStub.ttl = function(key, cb) { | ||
@@ -1180,11 +1219,37 @@ return cb(null, 5); | ||
}); | ||
it("retrieves data from cache and checks twice ttl without refreshing", function(done) { | ||
var funcCalled = 0; | ||
var values = []; | ||
for (var i = 0; i < 2; i++) { | ||
values.push(i); | ||
} | ||
async.eachSeries(values, function(val, next) { | ||
cache.wrap(key, function(cb) { | ||
funcCalled += 1; | ||
methods.getWidget(name, function(err, result) { | ||
cb(err, result); | ||
}); | ||
}, function(err, widget) { | ||
checkErr(err); | ||
assert.deepEqual(widget, {name: name}); | ||
next(err); | ||
}); | ||
}, function(err) { | ||
// Wait for just a bit, to be sure that the callback is called. | ||
setTimeout(function() { | ||
checkErr(err); | ||
assert.equal(memoryStoreStub.get.callCount, 2); | ||
assert.equal(memoryStoreStub.ttl.callCount, 2); | ||
assert.equal(funcCalled, 0); | ||
assert.equal(memoryStoreStub.set.callCount, 0); | ||
done(); | ||
}, 500); | ||
}); | ||
}); | ||
}); | ||
context("when result is already cached but expiring", function() { | ||
function getCachedWidget(name, cb) { | ||
cache.wrap(key, function(cacheCb) { | ||
methods.getWidget(name, cacheCb); | ||
}, opts, cb); | ||
} | ||
context("using tweaked memorystore, when result is already cached but expiring", function() { | ||
beforeEach(function(done) { | ||
@@ -1239,2 +1304,30 @@ memoryStoreStub.ttl = function(key, cb) { | ||
it("returns value and invokes worker in background using the provided ttl function", function(done) { | ||
var funcCalled = false; | ||
var ttlFuncCalled = false; | ||
var ttlFunc = function() { | ||
ttlFuncCalled = true; | ||
return 4; | ||
}; | ||
cache.wrap(key, function(cb) { | ||
funcCalled = true; | ||
methods.getWidget(name, function(err, result) { | ||
cb(err, result); | ||
}); | ||
}, {ttl: ttlFunc}, function(err, widget) { | ||
// Wait for just a bit, to be sure that the callback is called. | ||
setTimeout(function() { | ||
checkErr(err); | ||
assert.deepEqual(widget, {name: name}); | ||
assert.ok(memoryStoreStub.get.calledWith(key)); | ||
assert.ok(memoryStoreStub.ttl.calledWith(key)); | ||
assert.equal(funcCalled, true); | ||
assert.equal(ttlFuncCalled, true); | ||
assert.equal(memoryStoreStub.set.callCount, 1); | ||
done(); | ||
}, 500); | ||
}); | ||
}); | ||
it("returns value and invokes worker in background once when called multiple times", function(done) { | ||
@@ -1241,0 +1334,0 @@ var funcCalled = 0; |
Sorry, the diff of this file is too big to display
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
254674
5367