javascript - Select all values by pattern from redis server using node js -


i had multiple chanel this

client.hmset('live:user:1', {     "a": "1",     "b": "1" }); client.hmset('live:user:2', {     "a": "2",     "b": "2" }); client.hmset('live:user:3', {     "a": "3",     "b": "3" });  client.hmset('otherchanel:user:4', {     "a": "4",     "b": "4" }); client.hmset('otherchanel:user:5', {     "a": "5",     "b": "5" }); client.hmset('otherchanel:user:6', {     "a": "6",     "b": "6" }); 

how can select values key pattern "live:*"? need retern json object client app this:

[{user:1, a:1, b:1}, {user:2, a:2, b:2}, {user:3, a:3, b:3}] 

it's simple, have fetch keys , combine them. suggest using async make calls asynchronous:

function getallusersinchannel_suboptimal(channelname, functioncallback) { client.keys(channelname + ":user:*", function(err, userkeys) {     if (!err) {         async.map(userkeys,              function(userkey, callback) {                 client.hgetall(userkey, function(err, user) {                     if (!err) {                         callback(null, user);                     }                     else {                         callback(err);                     }                 });             },              function(err, userlist) {                 if (!err) {                     functioncallback(null, userlist);                 }                 else {                     functioncallback(err);                 }             });     }     else {         functioncallback(err);     } }); } 

using pattern match can drastically reduce performance on medium sized databases. suggest index id's of users using set.

create counter key if not exist , set value 0:

setnx <channel_name>:user:counter 0 

when add new user, first increment counter key, fetches value:

incr <channel_name>:user:counter => <new_id> 

this yield new id user. have save new member in "user:index" set so:

sadd <channel_name>:user:index <new_id> 

you can add new user key so:

hmset <channel_name>:user:<new_id> <list of values> 

to fetch data users first list of existing users so:

smembers <channel_name>:user:index => <list of user ids> 

for each id have fetch hash values:

asynchronous each <list of user ids> <user_id> hgetall channel_name>:user:<user_id> => values of key end asynchronous each 

in node.js, considering you've used techniques above index users, retrieve users in channel using this:

function getallusersinchannel_optimal(channelname, functioncallback) { client.smembers(channelname + ":user:index", function(err, userids) {     if (!err) {         async.map(userids,              function(userid, callback) {                 client.hgetall(channelname + ":user:" + userid, function(err, user) {                     if (!err) {                         callback(null, user);                     }                     else {                         callback(err);                     }                 });             },              function(err, userlist) {                 if (!err) {                     functioncallback(null, userlist);                 }                 else {                     functioncallback(err);                 }             });     }     else {         functioncallback(err);     } }); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -