Question

Suppose I have a SQL table that contains an field called userid (unique,Primary key, autoincrement), Username (varchar), and password (Varchar).

Now suppose I want to recreate this table in Redis. From my understanding, it's best to use a "hash" for this table. Thus my node.js code has a function that creates a user:

function newUser(username,password)
{
    //incr the userid
    client.incr("userid",redis.print);

    //Creating the user
    client.get('userid',function(err,userid){
       client.hmset("users", "id",userid,"name",username,"password",password);
    });

}

Is this correct?

Also, how do I access the information stored in his hash? I try the following but am only able to return 1 row:

client.hgetall('users',function(err,user){
   console.log('User Name: ' + user.name +" password = "+user.password);
})

Lastly, how would I join 2 hashes (like I would for 2 tables that needed to be joined?)

Était-ce utile?

La solution

first Redis is very flexible, so you can key names and data structure to use for users, try not approach this thinking in structured way like mySQL.

an integer to keep track of the user id.

global:index:users

storing user password as a string

user:<id>:password

storing user information in a hash

user:<id>

name=>username

email=>user email

some other key=>some other data

get userid by password, since we don't want to look entire user:<id> to get who's password is it.

user:<passwordHash>:id

of

user:<name>:id

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top