Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

socket.get multi returns a hash of values #646

Closed
wants to merge 2 commits into from

2 participants

@dshaw
socket.get('a','b','c', function (err, data) {
  socket.broadcast.emit('user info', data.a)
  socket.emit('profile', data)
})
@rauchg rauchg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
13 lib/stores/memory.js
@@ -83,7 +83,18 @@ Client.prototype.__proto__ = Store.Client;
*/
Client.prototype.get = function (key, fn) {
- fn(null, this.data[key] === undefined ? null : this.data[key]);
+ var data;
+ if (arguments.length == 2) data = (this.data[key] === undefined) ? null : this.data[key];
+ else {
+ var self = this
+ , args = Array.prototype.slice.call(arguments)
+ , fn = args.pop();
+ data = args.reduce(function(acc, k) {
+ acc[k] = (self.data[k] === undefined) ? null : self.data[k];
+ return acc;
+ }, {});
+ }
+ fn(null, data);
return this;
};
View
16 lib/stores/redis.js
@@ -211,7 +211,21 @@ Client.prototype.__proto__ = Store.Client;
*/
Client.prototype.get = function (key, fn) {
- this.store.cmd.hget(this.id, key, fn);
+ if (arguments.length == 2) this.store.cmd.hget(this.id, key, fn);
+ else {
+ var cmd = this.store.cmd
+ , args = Array.prototype.slice.call(arguments)
+ , fn = args.pop()
+ , callback = function(err, res) {
+ if (err) return fn(err);
+ var data = args.reduce(function(acc, arg, i) {
+ acc[arg] = (res[i] === undefined) ? null : res[i];
+ return acc;
+ }, {});
+ fn(null, data);
+ };
+ cmd.hmget.apply(cmd, [this.id].concat(args, callback));
+ }
return this;
};
View
22 test/stores.memory.test.js
@@ -71,6 +71,28 @@ module.exports = {
});
},
+ 'test multi get': function (done) {
+ var store = new MemoryStore
+ , client = store.client('test');
+
+ client.set('b', 'c', function (err) {
+ client.set('c', 'd', function (err) {
+ client.get('b','c', function (err, val) {
+ should.strictEqual(err, null);
+ should.deepEqual({'b':'c','c':'d'}, val);
+
+ client.get('a','b','c', function (err, val) {
+ should.strictEqual(err, null);
+ should.deepEqual({'a':null,'b':'c','c':'d'}, val);
+
+ store.destroy();
+ done();
+ });
+ });
+ });
+ });
+ },
+
'test cleaning up clients data': function (done) {
var rand1 = Math.abs(Math.random() * Date.now() | 0)
, rand2 = Math.abs(Math.random() * Date.now() | 0);
View
23 test/stores.redis.test.js
@@ -121,6 +121,29 @@ module.exports = {
});
},
+ 'test multi get': function (done) {
+ var store = new RedisStore
+ , rand = 'test-' + Date.now()
+ , client = store.client(rand);
+
+ client.set('b', 'c', function (err) {
+ client.set('c', 'd', function (err) {
+ client.get('b','c', function (err, val) {
+ should.strictEqual(err, null);
+ should.deepEqual({'b':'c','c':'d'}, val);
+
+ client.get('a','b','c', function (err, val) {
+ should.strictEqual(err, null);
+ should.deepEqual({'a':null,'b':'c','c':'d'}, val);
+
+ store.destroy();
+ done();
+ });
+ });
+ });
+ });
+ },
+
'test cleaning up clients data': function (done) {
var rand1 = Math.abs(Math.random() * Date.now() | 0)
, rand2 = Math.abs(Math.random() * Date.now() | 0);
Something went wrong with that request. Please try again.