cache-manager
Advanced tools
Comparing version 2.11.1 to 3.0.0
@@ -0,1 +1,4 @@ | ||
- 3.0.0 2020-02-21 | ||
-Added way to disable cloning of values in memory store (#135). | ||
- 2.11.1 2020-02-03 | ||
@@ -2,0 +5,0 @@ - Bugfix: Preserve function prototypes in memory store (#133). -@ryanbecker |
@@ -14,2 +14,13 @@ /*eslint no-unused-vars:0*/ | ||
/** | ||
* Wrapper for lru-cache. | ||
* NOTE: If you want to use a memory store, you want to write your own to have full | ||
* control over its behavior. E.g., you might need the extra Promise overhead or | ||
* you may want to clone objects a certain way before storing. | ||
* | ||
* @param {object} args - Args passed to underlying lru-cache, plus additional optional args: | ||
* @param {boolean} [args.shouldCloneBeforeSet=true] - Whether to clone the data being stored. | ||
* Default: true | ||
* @param {boolean} [args.usePromises=true] - Whether to enable the use of Promises. Default: true | ||
*/ | ||
var memoryStore = function(args) { | ||
@@ -20,3 +31,4 @@ args = args || {}; | ||
var Promise = args.promiseDependency || global.Promise; | ||
self.usePromises = !((typeof Promise === 'undefined' || args.noPromises)); | ||
self.usePromises = !(typeof Promise === 'undefined' || args.noPromises); | ||
self.shouldCloneBeforeSet = args.shouldCloneBeforeSet !== false; // clone by default | ||
@@ -45,3 +57,5 @@ var ttl = args.ttl; | ||
self.set = function(key, value, options, cb) { | ||
value = clone(value); | ||
if (self.shouldCloneBeforeSet) { | ||
value = clone(value); | ||
} | ||
@@ -48,0 +62,0 @@ if (typeof options === 'function') { |
{ | ||
"name": "cache-manager", | ||
"version": "2.11.1", | ||
"version": "3.0.0", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -126,2 +126,4 @@ [![build status](https://secure.travis-ci.org/BryanDonovan/node-cache-manager.svg)](http://travis-ci.org/BryanDonovan/node-cache-manager) | ||
// Note: callback is optional in set() and del(). | ||
// Note: memory cache clones values before setting them unless | ||
// shouldCloneBeforeSet is set to false | ||
@@ -128,0 +130,0 @@ memoryCache.set('foo', 'bar', {ttl: ttl}, function(err) { |
@@ -99,83 +99,173 @@ var assert = require('assert'); | ||
beforeEach(function() { | ||
cache = caching({store: 'memory', ttl: opts.ttl, ignoreCacheErrors: false}); | ||
// By default, memory store clones values before setting in the set method. | ||
context("when shouldCloneBeforeSet option is not passed in", () => { | ||
beforeEach(function() { | ||
cache = caching({store: 'memory', ttl: opts.ttl, ignoreCacheErrors: false}); | ||
}); | ||
it("does not allow mutation of objects", function(done) { | ||
getCachedObject('foo', function(err, result) { | ||
checkErr(err); | ||
result.foo = 'buzz'; | ||
getCachedObject('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result.foo, 'bar'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of arrays", function(done) { | ||
getCachedArray('foo', function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
result = ['a', 'b', 'c']; | ||
getCachedArray('foo', function(err, result) { | ||
checkErr(err); | ||
assert.deepEqual(result, [1, 2, 3]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of strings", function(done) { | ||
getCachedString('foo', function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
result = 'buzz'; | ||
getCachedString('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result, 'bar'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of numbers", function(done) { | ||
getCachedNumber('foo', function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
result = 12; | ||
getCachedNumber('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result, 34); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("preserves functions", function(done) { | ||
getCachedFunction('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(typeof result, 'function'); | ||
getCachedFunction('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(typeof result, 'function'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("preserves object prototype", function(done) { | ||
getCachedObjectWithPrototype('foo', function(err, result) { | ||
checkErr(err); | ||
assertCachedObjectWithPrototype(result); | ||
getCachedObjectWithPrototype('foo', function(err, result) { | ||
checkErr(err); | ||
assertCachedObjectWithPrototype(result); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of objects", function(done) { | ||
getCachedObject('foo', function(err, result) { | ||
checkErr(err); | ||
result.foo = 'buzz'; | ||
context("when shouldCloneBeforeSet=false option is passed in", () => { | ||
beforeEach(function() { | ||
cache = caching({store: 'memory', ttl: opts.ttl, shouldCloneBeforeSet: false, ignoreCacheErrors: false}); | ||
}); | ||
it("*does* allow mutation of objects", function(done) { | ||
getCachedObject('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result.foo, 'bar'); | ||
done(); | ||
result.foo = 'buzz'; | ||
getCachedObject('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result.foo, 'buzz'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of arrays", function(done) { | ||
getCachedArray('foo', function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
result = ['a', 'b', 'c']; | ||
it("does not allow mutation of arrays", function(done) { | ||
getCachedArray('foo', function(err, result) { | ||
checkErr(err); | ||
assert.deepEqual(result, [1, 2, 3]); | ||
done(); | ||
assert.ok(result); | ||
result = ['a', 'b', 'c']; | ||
getCachedArray('foo', function(err, result) { | ||
checkErr(err); | ||
assert.deepEqual(result, [1, 2, 3]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of strings", function(done) { | ||
getCachedString('foo', function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
result = 'buzz'; | ||
it("does not allow mutation of strings", function(done) { | ||
getCachedString('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result, 'bar'); | ||
done(); | ||
assert.ok(result); | ||
result = 'buzz'; | ||
getCachedString('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result, 'bar'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("does not allow mutation of numbers", function(done) { | ||
getCachedNumber('foo', function(err, result) { | ||
checkErr(err); | ||
assert.ok(result); | ||
result = 12; | ||
it("does not allow mutation of numbers", function(done) { | ||
getCachedNumber('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result, 34); | ||
done(); | ||
assert.ok(result); | ||
result = 12; | ||
getCachedNumber('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(result, 34); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("preserves functions", function(done) { | ||
getCachedFunction('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(typeof result, 'function'); | ||
it("preserves functions", function(done) { | ||
getCachedFunction('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(typeof result, 'function'); | ||
done(); | ||
getCachedFunction('foo', function(err, result) { | ||
checkErr(err); | ||
assert.equal(typeof result, 'function'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("preserves object prototype", function(done) { | ||
getCachedObjectWithPrototype('foo', function(err, result) { | ||
checkErr(err); | ||
assertCachedObjectWithPrototype(result); | ||
it("preserves object prototype", function(done) { | ||
getCachedObjectWithPrototype('foo', function(err, result) { | ||
checkErr(err); | ||
assertCachedObjectWithPrototype(result); | ||
done(); | ||
getCachedObjectWithPrototype('foo', function(err, result) { | ||
checkErr(err); | ||
assertCachedObjectWithPrototype(result); | ||
done(); | ||
}); | ||
}); | ||
@@ -182,0 +272,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
216989
4640
458