qcloud-connect-redis v4.0.4
qcloud-connect-redis is a Redis session store backed by node_redis, and is insanely fast :). Requires redis >= 2.0.0 for the SETEX command.
Update
use hashring to support multiple redis instance.
Setup
npm install qcloud-connect-redis express-sessionPass the express-session store into qcloud-connect-redis to create a RedisStore constructor.
var session = require('express-session');
var RedisStore = require('qcloud-connect-redis')(session);
app.use(session({
store: new RedisStore(options),
secret: 'keyboard cat'
}));Options
A Redis client is required. An existing client can be passed directly using the client param or created for you using the host, port, or socket params.
clientAn existing clienthostRedis server hostnameportRedis server portnosocketRedis server unix_socketurlRedis server url
The following additional params may be included:
serversSupport multiple redis instance.ttlRedis session TTL (expiration) in seconds. Defaults to session.maxAge (if set), or one day.disableTTLDisables setting TTL, keys will stay in redis until evicted by other means (overidesttl)dbDatabase index to use. Defaults to Redis's default (0).passPassword for Redis authenticationprefixKey prefix defaulting to "sess:"unrefSettrueto unref the Redis client. Warning: this is an experimental feature.serializerAn object containingstringifyandparsemethods compatible with Javascript'sJSONto override the serializer usedlogErrorsWhether or not to log client errors. (default:false)- If
true, a default logging function (console.error) is provided. - If a function, it is called anytime an error occurs (useful for custom logging)
- If
false, no logging occurs.
- If
// servers example
[
{
host: 'string',
port: 'number',
pass: 'string'
},
{
host: 'string',
port: 'number',
pass: 'string'
},
...
]Any options not included in this list will be passed to the redis createClient() method directly.
Custom Redis clients
Clients other than node_redis will work if they support the same interface. Just pass the client instance as the client configuration option. Known supported clients include:
- ioredis - adds support for Redis Sentinel and Cluster
FAQ
How do I handle lost connections to Redis?
By default, the node_redis client will auto-reconnect when a connection is lost. But requests may come in during that time. In express, one way this scenario can be handled is including a "session check" after setting up a session (checking for the existence of req.session):
app.use(session( /* setup session here */ ))
app.use(function (req, res, next) {
if (!req.session) {
return next(new Error('oh no')) // handle error
}
next() // otherwise continue
})If you want to retry, here is another option.
License
MIT