Redis: How to save (and read) Key-Value pairs at once by namespace/rule?

I want to utilize Redis for saving and reading a dynamic list of users.
Essentially, Redis is Key-Value pair storage. How can I read all the saved users at once? (for example, creating a namespace "users/user_id")

And since I am a Redis beginner,
Do you think the use of Redis in the above case is proper/efficient?

Thanks.

When using key/values for storing objects you should create a domain specific key by combining the domain name plus the unique id. For example, a user object that might look like this:

// typical user data model
var User = function(params) {
    if (!params) params = {};

    this.id = params.id;
    this.username = params.username;
    this.email = params.email;
    // other stuff...
};

Then domain key could be created like this:

var createUserDomainKey = function(id) {
    return 'User:' + id;
};

If the id was 'e9f6671440e111e49f14-77817cb77f36' the key would be this:

User:e9f6671440e111e49f14-77817cb77f36

Since redis will store string values, you need to serialize, probably with json so to save the user object. Assuming a valid use object would would do something like this:

var client = redis.createClient(),
    key = createUserDomainKey( user.id ),
    json = JSON.stringify( user ) ;

client.set( key, json, function(err, result) {
    if (err) throw err; // do something here...

    // result is 'OK'
});

For simple fire-hose queries returning all users, you can do this:

var client = redis.createClient();
client.keys( createUserDomainKey( '*' ), function(err, keys) {
    if (err) throw err; // do something here

    // keys contains all the keys matching 'User:*'
});

Note that the redis folks discourage the use of 'keys' for production, so a better approach is to build your own index using sorted-set, but if your user list is limited to a few hundred, there is no problem.

And since it returns a list of keys, you need to loop through and get each user then parse the json string to recover the real object. Assuming a populated list of keys, you could do something like this:

var client = redis.getClient(),
    users = [];

var loopCallback = function(err, value) {
    if (!err && value) {
        // parse and add the user model to the list
        users.push( JSON.parse( value ) );
    }

    // pull the next key, if available
    var key = keys.pop();

    if (key) {
        client.get( key, loopCallback );
    } else {
        // list is complete so return users, probably through a callback;
    }
}

// start the loop
loopCallback();

This is a good general purpose solution, but there are others that use sorted sets that are move efficient when you want access to the entire list with each access. This solution gives you the ability to get a single user object when you know the ID.

I hope this helps.

ps: a full implementation with unit tests of this can be found in the AbstractBaseDao module of this project.