5

I was told to use Redis for store authenticated users in my application on Heroku, so I decided to jump in today. What I want to do is store hashes of users in the Redis store like this:

{
   id:4532143215432,
   username:'davejlong',
   email:'[email protected]'
}

And then I want to be able to search by either username or id. Is this possible with Redis somehow?

I am using the node.js redis module which supports any redis command https://github.com/mranney/node_redis

2 Answers 2

3

There are few problems in @orangeoctopus usecae.

redis 127.0.0.1:6379> HMSET id:4532143215432 username davejlong [email protected] OK redis 127.0.0.1:6379> HMSET user:davejlong id 4532143215432 email [email protected] OK

This will make duplication, think about adding new values and deleting & updating.

So I prefer this

SET user:davejlong 1
HMSET user:1 username davejlong email [email protected] 

1) In case of username

 redis.get('user:davejlong',function(err,id){
     console.log('User Id of @davejlong: ' + id);
     redis.hgetall('user:'+id,function(err,user){
        console.log('User Data: ' + user);
     })
  })

2) In case of Id

   redis.hgetall('user:1',function(err,user){
       console.log('User Data: ' + user);
    })
Sign up to request clarification or add additional context in comments.

2 Comments

I eluded to this with "A more compact way of doing this..." but was too lazy to type it out. +1
Sorry I missed the point since you are not adding the ability to find by mail. SET user:[email protected] 1 and you are able to find by mail
3

It's as simple as storing each user twice. Once with the key of id and once with the key of username.

A more compact way of doing this in terms of memory is to have usernames key to ids, so your username query would like like: query by username, get id; put in id, get info.

Unfortunately, there isn't a good way to have the same actual data be keyed on by two different keys.


For example, when you would insert a new user and then query for it:

redis 127.0.0.1:6379> HMSET id:4532143215432 username davejlong email [email protected]
OK
redis 127.0.0.1:6379> HMSET user:davejlong id 4532143215432 email [email protected]
OK
redis 127.0.0.1:6379> HGET id:4532143215432 username
"davejlong"
redis 127.0.0.1:6379> HGET user:davejlong id
"4532143215432"
redis 127.0.0.1:6379> HMGET user:davejlong email id
1) "[email protected]"
2) "4532143215432"
redis 127.0.0.1:6379> DEL user:davejlong
(integer) 1
redis 127.0.0.1:6379> DEL id:4532143215432
(integer) 1

Notice that when I am creating the user, I use HMSET twice. Now, I can query against either the username or the id. I also have to delete both keys now.

2 Comments

Could you show me how I would do that in the CLI? I'm brand new to all this redis stuff, so I'm not sure whether to use a hash, list, etc.
You are creating duplicate data here. Consider the use indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.