Node+redis实现

1.redis 作为存储服务器###

redis.js

var redis = require('redis');
var bluebird = require('bluebird');
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
var options = {
    host: 'host'
};
var client = redis.createClient(options);

client.on('ready', function () {
    console.log('connected');
});
module.exports = client;

redis-user-dao.js

// user related
var setUserBuild = function (user) {
    return redisClient.hsetAsync([user.id, 'build', JSON.stringify(user.build)]);
};

var setUserConfig = function (id, key, val) {
    return redisClient.hsetAsync([id, key, val]);
};

var getUserInfo = function (id) {
    return redisClient.hgetallAsync([id]);
};

2.redis作为session服务器###

app.js里配置session

var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var final_session_setting=session({
  store: new RedisStore({
    client:client   //client 为 redis.js 里 export 的 client
  }),
  secret: 'keyboard cat',
  cookie: { maxAge: 259200000 }
});
app.use(final_session_setting)

你可能感兴趣的:(Node+redis实现)