Make Node Redis get () Synchronous

I just started implementing redis using node. during the implementation of the authentication method, I need to check if a token exists in redis, if I do not update the new token in redis and in my mongo db, since I need to write a large callback block and not get the result properly. how can we make redis red from callbacks. how can we do this synchronously. sample code below.

module.exports.authenticate = function(request, response) { var reply = {}; if(UserSchema) { var UserModel, attributes; /** Registering User Model; **/ mongoose.model('user', UserSchema); UserModel = mongoose.model('user'); attributes = request.params; UserModel.findOne(attributes, "_id name email token", function(error, user) { if(!error && user) { var token; //delete user.password; token = user.token; /** Checking token exists in redis; **/ redisClient.get(token, function(error, value) { if(value === null && error === null) { /** Creating new token; **/ token = require('crypto').createHash('md5').update("" + (new Date()).getTime()).digest("hex"); user.token = token; /** Storing new token on redis; **/ setTokenOnRedis(token); /** Updating token in the user model; **/ UserModel.update({ _id : user._id}, { token : token }, function(error, user) { if(error !== null && user === null) { deleteTokenOnRedis(token); /** Error message; **/ reply = { error : true, code : "AUTH#001", msg : "User authentication failed, Please check user credentials." } response.send(reply); }else if(error === null && user !== null) { reply = user; response.send(reply); } }); }else if(value !== null) { reply = user; response.send(reply); }else { /** Error message; **/ reply = { error : true, code : "AUTH#001", msg : "User authentication failed, Please check user credentials." }; response.send(reply); } }); }else { /** Error message; **/ reply = { error : true, code : "AUTH#001", msg : "User authentication failed, Please check user credentials." } } }); }else { /** Error message; **/ reply = { error : true, code : "AUTH#001", msg : "User authentication failed, Please check user credentials." } response.send(reply); } }; 
+4
source share
1 answer

No, you cannot make any synchronous io calls, including redis. The only synchronous io calls I know are the file system and console.

However, there are some encoding methods that you can use to make asynchronous encoding more manageable.

  • come back earlier by checking the error first.
  • move the repeating code into a separate function, for example, create error structures.
  • use this asynchronous library: https://github.com/caolan/async . In particular, a waterfall function may be convenient here.

I also think that you will need to pass a callback method to these functions, since they are asynchronous.

  • setTokenOnRedis (token);
  • deleteTokenOnRedis (token);

I reorganized your sample code, which I hope should be less indented and more readable / supported. I have not used async, I will leave it to you.

Personally, I found that the entire asynchronous node coding model is very frustrating, but you get used to it. After some time, you will learn to use various asynchronous coding patterns, and then it becomes almost valid :)

Some links that may help you:

reorganized code:

 module.exports.authenticate = function(request, response){ authenticate(request, response, function(err, reply){ if(err){ reply = authenticationError(err); } response.send(reply); }); }; var authenticationError = function(internalmsg){ return { internalmsg : internalmsg, error : true, code : "AUTH#001", msg : "User authentication failed, Please check user credentials." }; }; var authenticate = function(request, response, callback) { if(UserSchema) { var UserModel, attributes; /** Registering User Model; **/ mongoose.model('user', UserSchema); UserModel = mongoose.model('user'); attributes = request.params; UserModel.findOne(attributes, "_id name email token", function(err, user) { if(err || !user){ return callback(err || "UserModel.findOne, no user"); } var token; //delete user.password; token = user.token; /** Checking token exists in redis; **/ redisClient.get(token, function(err, value){ if(err){ return callback(err); } if(value){ return callback(null, value); } /** Creating new token; **/ token = require('crypto').createHash('md5').update("" + (new Date()).getTime()).digest("hex"); user.token = token; /** Storing new token on redis; **/ setTokenOnRedis(token); /** Updating token in the user model; **/ UserModel.update({ _id : user._id}, { token : token }, function(err, user) { if(err || !user) { deleteTokenOnRedis(token); return callback(err || "UserModel.update, no user found"); } callback(null, user); }); }); }); } }; 
+12
source

All Articles