Skip to content

Commit dd78546

Browse files
authored
Merge pull request #48 from jeff-kilbride/feature/undefined_json
Feature/undefined json
2 parents 66b236f + 2ea110b commit dd78546

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ function redisStore(args) {
7272
}
7373

7474
if (opts.parse) {
75-
7675
try {
77-
// allow undefined only if allowed by isCacheableValue
78-
if(! ( (result === undefined || result === 'undefined') && typeof args.isCacheableValue === 'function' && args.isCacheableValue(result))) {
79-
result = JSON.parse(result);
80-
}
76+
result = JSON.parse(result);
8177
} catch (e) {
8278
return cb && cb(e);
8379
}
@@ -187,7 +183,7 @@ function redisStore(args) {
187183
options = {};
188184
}
189185

190-
if (!value && !self.isCacheableValue(value)) {
186+
if (!self.isCacheableValue(value)) {
191187
return cb(new Error('value cannot be ' + value));
192188
}
193189

@@ -199,7 +195,7 @@ function redisStore(args) {
199195
if (err) {
200196
return cb && cb(err);
201197
}
202-
var val = JSON.stringify(value) || 'undefined';
198+
var val = JSON.stringify(value) || '"undefined"';
203199
if (ttl) {
204200
conn.setex(key, ttl, val, handleResponse(conn, cb));
205201
} else {

test/lib/redis-store-spec.js

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

66
var redisCache;
77
var customRedisCache;
8+
var customRedisCache2;
89

910
before(function () {
1011
redisCache = require('cache-manager').caching({
@@ -28,6 +29,19 @@ before(function () {
2829
return redisCache.store.isCacheableValue(val);
2930
}
3031
});
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) {
40+
// disallow FooBarString
41+
if (val === 'FooBarString') return false;
42+
return redisCache.store.isCacheableValue(val);
43+
}
44+
});
3145
});
3246

3347
describe ('initialization', function () {
@@ -137,6 +151,19 @@ describe('set', function () {
137151
});
138152
});
139153

154+
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) {
157+
try {
158+
assert.notEqual(err, null);
159+
assert.equal(err.message, 'value cannot be FooBarString');
160+
done();
161+
} catch (e) {
162+
done(e);
163+
}
164+
});
165+
});
166+
140167
});
141168

142169
describe('get', function () {

0 commit comments

Comments
 (0)