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
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”).
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.
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.
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);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With