Last active
December 21, 2015 06:49
-
-
Save mccxj/6266733 to your computer and use it in GitHub Desktop.
Redis Object Mapping, Object style, NoSQL inside.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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