使用connect-redis
const redis = require('redis')
const session = require('express-session')
let RedisStore = require('connect-redis')(session)
let redisClient = redis.createClient(6379, 'localhost')
app.use(
session({
store: new RedisStore({ client: redisClient }),
saveUninitialized: false,
secret: 'keyboard cat',
resave: false,
})
)
上面使用后session就直接是redis里面了。
redis集群的使用
redis不支持集群这个时候使用ioredis
const Redis = require("ioredis");
const session = require('express-session')
let RedisStore = require('connect-redis')(session)
const cluster = new Redis.Cluster([
{
port: 6380,
host: "127.0.0.1",
},
{
port: 6381,
host: "127.0.0.1",
},
]);
app.use(
session({
store: new RedisStore({ client: cluster }),
saveUninitialized: false,
secret: 'keyboard cat',
resave: false,
})
)