cache-manager
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -0,1 +1,4 @@ | ||
- 1.2.1 2015-10-17 | ||
- Bugfix: multi-caching: using underlying store's isCacheableValue function when it exists (#34). | ||
- 1.2.0 2015-10-07 | ||
@@ -2,0 +5,0 @@ - using `isCacheableValue` in `getFromHighestPriorityCache` and `getAndPassUp` (#32). |
@@ -15,2 +15,5 @@ /** @module cacheManager/multiCaching */ | ||
* stored in cache. By default it caches everything except undefined. | ||
* | ||
* If an underlying cache specifies its own isCacheableValue function, that function will | ||
* be used instead of the multiCaching's _isCacheableValue function. | ||
*/ | ||
@@ -35,2 +38,15 @@ var multiCaching = function(caches, options) { | ||
/** | ||
* If the underlying cache specifies its own isCacheableValue function (such | ||
* as how node-cache-manager-redis does), use that function, otherwise use | ||
* self._isCacheableValue function. | ||
*/ | ||
function getIsCacheableValueFunction(cache) { | ||
if (cache.store && typeof cache.store.isCacheableValue === 'function') { | ||
return cache.store.isCacheableValue; | ||
} else { | ||
return self._isCacheableValue; | ||
} | ||
} | ||
function getFromHighestPriorityCache(key, options, cb) { | ||
@@ -48,3 +64,6 @@ if (typeof options === 'function') { | ||
} | ||
if (self._isCacheableValue(result)) { | ||
var _isCacheableValue = getIsCacheableValueFunction(cache); | ||
if (_isCacheableValue(result)) { | ||
// break out of async loop. | ||
@@ -65,3 +84,9 @@ return cb(err, result, i); | ||
async.each(caches, function(cache, next) { | ||
cache.store.set(opts.key, opts.value, opts.options, next); | ||
var _isCacheableValue = getIsCacheableValueFunction(cache); | ||
if (_isCacheableValue(opts.value)) { | ||
cache.store.set(opts.key, opts.value, opts.options, next); | ||
} else { | ||
next(); | ||
} | ||
}, cb); | ||
@@ -85,7 +110,10 @@ } | ||
if (self._isCacheableValue(result) && index) { | ||
if (index) { | ||
var cachesToUpdate = caches.slice(0, index); | ||
async.each(cachesToUpdate, function(cache, next) { | ||
// We rely on the cache module's default TTL | ||
cache.set(key, result, next); | ||
var _isCacheableValue = getIsCacheableValueFunction(cache); | ||
if (_isCacheableValue(result)) { | ||
// We rely on the cache module's default TTL | ||
cache.set(key, result, next); | ||
} | ||
}); | ||
@@ -92,0 +120,0 @@ } |
{ | ||
"name": "cache-manager", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -537,2 +537,77 @@ var assert = require('assert'); | ||
}); | ||
context("when an underlying store has its own isCacheableValue function", function() { | ||
var memoryCache4; | ||
var testCallbacks = { | ||
isCacheableValue: function(value) { | ||
var x = value !== 'do_not_store_this' && value !== undefined; | ||
return x; | ||
} | ||
}; | ||
function getValue(name, cb) { | ||
process.nextTick(function() { | ||
if (name === 'foo') { | ||
cb(null, 'store_this'); | ||
} else { | ||
cb(null, 'do_not_store_this'); | ||
} | ||
}); | ||
} | ||
function getCachedValue(name, cb) { | ||
multiCache.wrap(key, function(cacheCb) { | ||
getValue(name, function(err, result) { | ||
cacheCb(err, result); | ||
}); | ||
}, cb); | ||
} | ||
beforeEach(function() { | ||
sinon.spy(testCallbacks, 'isCacheableValue'); | ||
memoryCache4 = caching({ | ||
store: 'memory', | ||
ttl: memoryTtl | ||
}); | ||
// This simulates how node-cache-manager-redis sets its | ||
// isCacheableValue function: | ||
memoryCache4.store.isCacheableValue = testCallbacks.isCacheableValue; | ||
multiCache = multiCaching([memoryCache4]); | ||
sinon.spy(memoryCache4.store, 'set'); | ||
}); | ||
afterEach(function() { | ||
memoryCache4.store.set.restore(); | ||
testCallbacks.isCacheableValue.restore(); | ||
}); | ||
it("stores allowed values", function(done) { | ||
var name = 'foo'; | ||
getCachedValue(name, function(err) { | ||
checkErr(err); | ||
assert.ok(memoryCache4.store.set.called); | ||
assert.ok(testCallbacks.isCacheableValue.called); | ||
getCachedValue(name, function(err) { | ||
checkErr(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("does not store non-allowed values", function(done) { | ||
var name = 'bar'; | ||
getCachedValue(name, function(err) { | ||
checkErr(err); | ||
assert.ok(memoryCache4.store.set.notCalled); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -539,0 +614,0 @@ |
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
116708
2475