node redis get是异步取,如何同步取值?
果断用express.js 支持的reids存储session
var express = require(‘express’) , RedisStore = require(‘connect-redis’)(express) , sessionStore = new RedisStore();
app.configure(function(){ app.set('views’, __dirname + ‘/views’); app.set('view engine’, ‘html’); app.use(partials()); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.static(__dirname + ‘/public’)); app.use(express.cookieParser()); app.use(express.session({secret:’1w2q’, store: sessionStore, cookie:{maxAge:config.SESSIONEXPIRESTIME}})); app.use(app.router); });
同步貌似不好搞,express 内步好像是这样的你设置了session也不立刻存到session服务器中,会在req 解发end之件之后写到session服务器中
// proxy end() to commit the session var end = res.end; res.end = function(data, encoding){ res.end = end; if (req.session) { // HACK: ensure Set-Cookie for implicit writeHead() if (!res._header) res._implicitHeader(); req.session.resetMaxAge(); req.session.save(function(){ res.end(data, encoding); }); } else { res.end(data, encoding); } };
// generate the session object var pause = utils.pause(req); store.get(req.sessionID, function(err, sess){ // proxy to resume() events var _next = next; next = function(err){ _next(err); pause.resume(); }
// error handling
if (err) {
if ('ENOENT' == err.code) {
generate();
next();
} else {
next(err);
}
// no session
} else if (!sess) {
generate();
next();
// populate req.session
} else {
store.createSession(req, sess);
next();
}