how to get all users in redis

2,753 views
Skip to first unread message

Rahul Mehta

unread,
Jun 3, 2011, 2:34:53 AM6/3/11
to nodejs
I have the following code .

var redis = require("redis"),
client = redis.createClient();


user_rahul = {
username: 'rahul'

};
user_namita = {
username: 'namita'
};
client.hmset('users.rahul', user_rahul);
client.hmset('users.namita', user_namita);
var username = "rahul"; // From a POST perhaps
client.hgetall("users" , function(err, user) {
console.log(user);
});

I want to get all the users list how i can get all users list this
what i tried but its not working.

S. A.

unread,
Jun 3, 2011, 2:44:52 AM6/3/11
to nod...@googlegroups.com
Using the command line 'redis-cli' you can get all user list using the command: keys users*
I have not used the node client yet, but I would assume that it would be something along the above command line.

Matt Ranney

unread,
Jun 4, 2011, 11:11:58 PM6/4/11
to nod...@googlegroups.com
If you expect your database to grow to a large number of keys, you'll probably need to avoid using the "keys" command.  It's fine to use for now when you are experimenting.

I'm not sure what is the best thing for your application, but if you want to efficiently keep a list of unique user ids, then you probably want a set like this:

// add some users
client.sadd("users", "rahul");
client.sadd("users", "namita");

// list all users
client.smembers("users", redis.print);


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


S. A.

unread,
Jun 7, 2011, 11:04:42 AM6/7/11
to nod...@googlegroups.com
> If you expect your database to grow to a large number of keys, you'll probably need to avoid using the "keys" command. 
> It's fine to use for now when you are experimenting.

I agree with the above statement in a normal database, where you will have means to setup some sort of pagination and loop thru' subsets of data to get to the whole set. In redis though, I am not sure if you can chuck and get pieces of the whole in a loop; if you can't get subsets, keys is the only way to get all the records. Using anything other than 'keys' command is probably inefficient. Of course, you should always use the right prefix as a the parameter to the keys command and the best way to limiting the results is to provide longest possible set of chars for key (followed by a *): keys users*

Matt Ranney

unread,
Jun 8, 2011, 1:38:46 AM6/8/11
to nod...@googlegroups.com
Once you start needing pagination of your redis queries, you just need a secondary index like a sorted set that keeps your keys sorted by some index.  Node and redis are both generally pretty fast, so the point where you actually NEED to do this is perhaps further off than it might be with other systems.  Thankfully, there are very clean ways to model these kinds of structures in Redis.

S. A.

unread,
Jun 8, 2011, 4:28:52 PM6/8/11
to nod...@googlegroups.com
I don't quite follow the need for user building a secondary index in the quest for accessing all records that match a given key.  We are talking about a single key here. My knowledge in redis is really limited, but in a regular DBs I can see a need for secondary key to help the search process if you use multiple keys in a search. In such a case, even if user does not build these, DB systems internally build the secondary keys, but that is different story. You said:
> Thankfully, there are very clean ways to model these kinds of structures in Redis.
Could you post some links on these so I can catch up with redis.

Reply all
Reply to author
Forward
0 new messages