Skip to content

Commit

Permalink
[minor] binary pack buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaagie committed Sep 8, 2015
1 parent e04391f commit 91addd5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/redis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var fuse = require('fusing');
var fuse = require('fusing')
, binary = require('binarypack');

/**
* Setup Redis persistence layer via node-redis.
Expand Down Expand Up @@ -66,7 +67,10 @@ Redis.readable('api', function api(dynamis) {
*/
dynamis.api('get', function get(key, done) {
dynamis.execute(redis.database, redis.database.get, key, function complete(error, value) {
try { value = JSON.parse(value); } catch (err) { error = error || err; }
try {
if (Buffer.isBuffer(value)) value = binary.unpack(value);
else value = JSON.parse(value);
} catch (err) { error = error || err; }
done(error, value);
});
});
Expand All @@ -86,7 +90,11 @@ Redis.readable('api', function api(dynamis) {
ttl = null;
}

try { value = JSON.stringify(value); } catch (error) { return done(error); }
try {
if (Buffer.isBuffer(value)) value = binary.pack(value);
else value = JSON.stringify(value);
} catch (error) { return done(error); }

if (!ttl) return dynamis.execute(redis.database, redis.database.set, key, value, done);
dynamis.execute(redis.database, redis.database.setex, key, ttl, value, done);
});
Expand Down
2 changes: 1 addition & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.redis = function () {

exports.levelup = function () {
return require('levelup')('/tmp/test');
}
};

exports.cradle = function () {
return new require('cradle').Connection;
Expand Down
21 changes: 21 additions & 0 deletions test/lib/redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ describe('Redis persistence layer', function () {
'use strict';

var common = require('../common')
, binary = require('binarypack')
, expect = common.expect
, sinon = common.sinon
, Dynamis = common.Dynamis
Expand Down Expand Up @@ -132,6 +133,26 @@ describe('Redis persistence layer', function () {
done();
});
});

it('will binary-pack buffers', function (done) {
var data = new Buffer('test');

redis = require('redis').createClient({ return_buffers: true });
dynamis = new Dynamis('redis', redis, { database: 10 });
persist = new Persist(dynamis, redis);

dynamis.set(key, data, function (error, result) {
expect(error).to.equal(null);
expect(result.toString()).to.equal('OK');

redis.get(key, function (error, result) {
expect(error).to.equal(null);
expect(binary.unpack(result).length).to.equal(data.length);
expect(binary.unpack(result).toString()).to.equal(data.toString());
done();
});
});
});
});

describe('#get', function () {
Expand Down

0 comments on commit 91addd5

Please sign in to comment.