Skip to content

Commit 9f5bc1e

Browse files
author
Joe Podwys
authored
Merge pull request #13 from peakon/master
pass JSON object to redisCacheModule
2 parents 283f025 + f5772e0 commit 9f5bc1e

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

redisCacheModule.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ function redisCacheModule(config){
3636
self.backgroundRefreshIntervalCheck = (typeof config.backgroundRefreshIntervalCheck === 'boolean') ? config.backgroundRefreshIntervalCheck : true;
3737
self.backgroundRefreshInterval = config.backgroundRefreshInterval || 60000;
3838
self.backgroundRefreshMinTtl = config.backgroundRefreshMinTtl || 70000;
39+
self.JSON = config.JSON || Object.create(JSON);
3940
self.logJsonParseFailures = config.logJsonParseFailures || false;
41+
4042
var refreshKeys = {};
4143
var backgroundRefreshEnabled = false;
4244

@@ -134,7 +136,7 @@ function redisCacheModule(config){
134136
log(false, 'Attempting to get key:', {key: cacheKey});
135137
self.db.get(cacheKey, function(err, result){
136138
try {
137-
result = JSON.parse(result);
139+
result = self.JSON.parse(result);
138140
} catch (err) {
139141
if(self.logJsonParseFailures) {
140142
log(true, 'Error parsing JSON, err:', err);
@@ -163,7 +165,7 @@ function redisCacheModule(config){
163165
for(var i = 0; i < response.length; i++){
164166
if(response[i] !== null){
165167
try {
166-
response[i] = JSON.parse(response[i]);
168+
response[i] = self.JSON.parse(response[i]);
167169
} catch (err) {
168170
if(self.logJsonParseFailures) {
169171
log(true, 'Error parsing JSON, err:', err);
@@ -201,7 +203,7 @@ function redisCacheModule(config){
201203
var exp = (expiration * 1000) + Date.now();
202204
if(typeof value === 'object'){
203205
try {
204-
value = JSON.stringify(value);
206+
value = self.JSON.stringify(value);
205207
} catch (err) {
206208
if(self.logJsonParseFailures) {
207209
log(true, 'Error converting to JSON, err:', err);
@@ -252,7 +254,7 @@ function redisCacheModule(config){
252254
value = value.cacheValue;
253255
}
254256
try {
255-
value = JSON.stringify(value);
257+
value = self.JSON.stringify(value);
256258
} catch (err) {
257259
if(self.logJsonParseFailures) {
258260
log(true, 'Error converting to JSON, err:', err);
@@ -342,13 +344,13 @@ function redisCacheModule(config){
342344
if (self.redisData) {
343345
if(typeof self.redisData === 'string'){
344346
self.db = redis.createClient(self.redisData,
345-
{'no_ready_check': true,
347+
{'no_ready_check': true,
346348
retry_strategy: retryStrategy});
347349
} else {
348350
self.db = redis.createClient(self.redisData.port,
349-
self.redisData.hostname, {'no_ready_check': true,
351+
self.redisData.hostname, {'no_ready_check': true,
350352
retry_strategy: retryStrategy});
351-
353+
352354
// don't call redis auth method if no auth info passed
353355
if (self.redisData.auth) {
354356
self.db.auth(self.redisData.auth);

test/server/redis-cache-module.js

+19
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,26 @@ describe('redisCacheModule Tests', function () {
154154

155155
}, 1500);
156156
});
157+
it('Using custom JSON interface should parse JSON to custom object', function (done) {
158+
this.timeout(5000);
159+
160+
redisCache.logJsonParseFailures = true;
161+
redisCache.JSON.parse = function (text) {
162+
const obj = JSON.parse(text);
163+
if (obj.type === 'Buffer') {
164+
return Buffer.from(obj);
165+
} else {
166+
return obj;
167+
}
168+
};
157169

170+
const buffer = Buffer.from([0x00, 0x61, 0x00, 0x62, 0x00, 0x63])
171+
redisCache.set('bffr', buffer);
172+
redisCache.get('bffr', function (err, response) {
173+
expect(response).toEqual(buffer);
174+
done();
175+
});
176+
});
158177
it('should retry connecting when retries is less than 5 times', function() {
159178
var mockOptions = {
160179
attempt: 5,

0 commit comments

Comments
 (0)