Skip to content

Commit 8701d62

Browse files
jeff-kilbridePuKoren
authored andcommitted
Feature/store null values (#50)
* Fix test for storing null value. Fix isCacheableValue to allow null. Change isCacheableValue tests. * Update comments for isCacheableValue.
1 parent 8017a1d commit 8701d62

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ function redisStore(args) {
277277
/**
278278
* Specify which values should and should not be cached.
279279
* If the function returns true, it will be stored in cache.
280-
* By default, it caches everything except null and undefined values.
280+
* By default, it caches everything except undefined values.
281281
* Can be overriden via standard node-cache-manager options.
282282
* @method isCacheableValue
283283
* @param {String} value - The value to check
284284
* @return {Boolean} - Returns true if the value is cacheable, otherwise false.
285285
*/
286286
self.isCacheableValue = args.isCacheableValue || function(value) {
287-
return value !== null && value !== undefined;
287+
return value !== undefined;
288288
};
289289

290290
/**

test/lib/redis-store-spec.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var assert = require('assert');
55

66
var redisCache;
77
var customRedisCache;
8-
var customRedisCache2;
98

109
before(function () {
1110
redisCache = require('cache-manager').caching({
@@ -25,20 +24,11 @@ before(function () {
2524
ttl: config.redis.ttl,
2625
isCacheableValue: function (val) {
2726
// allow undefined
28-
if (val === undefined) return true;
29-
return redisCache.store.isCacheableValue(val);
30-
}
31-
});
32-
33-
customRedisCache2 = require('cache-manager').caching({
34-
store: redisStore,
35-
host: config.redis.host,
36-
port: config.redis.port,
37-
db: config.redis.db,
38-
ttl: config.redis.ttl,
39-
isCacheableValue: function (val) {
27+
if (val === undefined)
28+
return true;
4029
// disallow FooBarString
41-
if (val === 'FooBarString') return false;
30+
else if (val === 'FooBarString')
31+
return false;
4232
return redisCache.store.isCacheableValue(val);
4333
}
4434
});
@@ -92,20 +82,20 @@ describe('set', function () {
9282
done();
9383
});
9484
});
85+
});
9586

96-
it('should store a null value without error', function (done) {
97-
redisCache.set('foo2', null, function (err) {
98-
try {
87+
it('should store a null value without error', function (done) {
88+
redisCache.set('foo2', null, function (err) {
89+
try {
90+
assert.equal(err, null);
91+
redisCache.get('foo2', function (err, value) {
9992
assert.equal(err, null);
100-
redisCache.get('foo2', function (err, value) {
101-
assert.equal(err, null);
102-
assert.equal(value, null);
103-
done();
104-
});
105-
} catch (e) {
106-
done(e);
107-
}
108-
});
93+
assert.equal(value, null);
94+
done();
95+
});
96+
} catch (e) {
97+
done(e);
98+
}
10999
});
110100
});
111101

@@ -130,7 +120,7 @@ describe('set', function () {
130120
});
131121
});
132122

133-
it('should store an undefined value if permitted by isCacheableValue', function (done) {
123+
it('should store an undefined value if permitted by isCacheableValue', function (done) {
134124
assert(customRedisCache.store.isCacheableValue(undefined), true);
135125
customRedisCache.set('foo3', undefined, function (err) {
136126
try {
@@ -152,8 +142,8 @@ describe('set', function () {
152142
});
153143

154144
it('should not store a value disallowed by isCacheableValue', function (done) {
155-
assert.strictEqual(customRedisCache2.store.isCacheableValue('FooBarString'), false);
156-
customRedisCache2.set('foobar', 'FooBarString', function (err) {
145+
assert.strictEqual(customRedisCache.store.isCacheableValue('FooBarString'), false);
146+
customRedisCache.set('foobar', 'FooBarString', function (err) {
157147
try {
158148
assert.notEqual(err, null);
159149
assert.equal(err.message, 'value cannot be FooBarString');
@@ -341,16 +331,16 @@ describe('keys', function () {
341331
});
342332

343333
describe('isCacheableValue', function () {
344-
it('should return true when the value is not null or undefined', function (done) {
334+
it('should return true when the value is not undefined', function (done) {
345335
assert.equal(redisCache.store.isCacheableValue(0), true);
346336
assert.equal(redisCache.store.isCacheableValue(100), true);
347337
assert.equal(redisCache.store.isCacheableValue(''), true);
348338
assert.equal(redisCache.store.isCacheableValue('test'), true);
339+
assert.equal(redisCache.store.isCacheableValue(null), true);
349340
done();
350341
});
351342

352-
it('should return false when the value is null or undefined', function (done) {
353-
assert.equal(redisCache.store.isCacheableValue(null), false);
343+
it('should return false when the value is undefined', function (done) {
354344
assert.equal(redisCache.store.isCacheableValue(undefined), false);
355345
done();
356346
});

0 commit comments

Comments
 (0)