Skip to content

Commit

Permalink
updates based on comments on #15
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Sep 9, 2014
1 parent a31298d commit 24666de
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ internals.MemoryCacheEntry = function MemoryCacheEntry (key, value, ttl, allowMi
// copy buffer to prevent value from changing while in the cache
value.copy(this.item);
valueByteSize = this.item.length;
} else {
}
else {
// stringify() to prevent value from changing while in the cache
this.item = JSON.stringify(value);
valueByteSize = Buffer.byteLength(this.item);
Expand Down Expand Up @@ -113,16 +114,16 @@ internals.Connection.prototype.get = function (key, callback) {
}

var value = null;
try {
if (Buffer.isBuffer(envelope.item)) {
value = envelope.item;
}
else {
value = JSON.parse(envelope.item);
}

if (Buffer.isBuffer(envelope.item)) {
value = envelope.item;
}
catch (err) {
return callback(new Error('Bad value content'));
else {
value = internals.parseJSON(envelope.item);

if (value instanceof Error) {
return callback(new Error('Bad value content'));
}
}

var result = {
Expand Down Expand Up @@ -207,4 +208,19 @@ internals.Connection.prototype.drop = function (key, callback) {
}

return callback();
};
};


internals.parseJSON = function (json) {

var obj = null;

try {
obj = JSON.parse(json);
}
catch (err) {
obj = err;
}

return obj;
};

0 comments on commit 24666de

Please sign in to comment.