Skip to content

Instantly share code, notes, and snippets.

@mccxj
Last active December 21, 2015 06:49
Show Gist options
  • Save mccxj/6266733 to your computer and use it in GitHub Desktop.
Save mccxj/6266733 to your computer and use it in GitHub Desktop.
Redis Object Mapping, Object style, NoSQL inside.
//assume user table with 4 columns: id, name, email, last_login. id is the key column.
//give id, then we fetch from redis like : users:id:name, users:id:email, users:id:last_login.
//maybe we should return a user object to client.
var async = require('async');
var db = require('redis').createClient();
var user = {};
var id = 1; // fetch from request parameter
var column_fetch = function(id, column, callback) {
return function(callback){
db.get('users:'+id+':'+column, function(err, column){
if(err) {
callback(err, null);
return;
}
callback(null, column);
});
};
}
exports.fetch = function(id, cb){
async.parallel({
name: column_fetch(id, 'name', cb),
email: column_fetch(id, 'email', cb),
last_login: column_fetch(id, 'last_login', cb)
},cb);
}
// client
fetch(1, function(err, user){
if(err)
console.error(err);
else
console.info(user);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment