Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching by multiple values within a redis key/value

Tags:

node.js

redis

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

like image 553
Dave Long Avatar asked Oct 28 '11 18:10

Dave Long


People also ask

Can one key have multiple values in Redis?

In Redis you can also have lists as the value, which overcomes the problem of having more than one value for a key, as you can have an ordered list with multiple values (so “they'll none of 'em be missed”).

How do I find my Redis key-value?

To get the value stored in a key, you can use the GET command followed by the name of the key. The above command tells Redis to fetch the value stored in the specified key. We can use the GET command followed by the unique value as: GET username:3.

Is Redis key-value pair?

A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values. Hence, they are used to represent objects.


1 Answers

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);
    })
like image 76
Ganesh Kumar Avatar answered Sep 27 '22 16:09

Ganesh Kumar