connect-redis
Redis session store for Connect
Last updated 2 months ago by wavded .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install connect-redis 
SYNC missed versions from official npm registry.

npm travis code-style Downloads

connect-redis provides Redis session storage for Express. Requires Redis >= 2.0.0.

Migrating to V4? See this guide on what's changed.

Installation

npm:

npm install redis connect-redis express-session

Yarn:

yarn add redis connect-redis express-session

API

const redis = require('redis')
const session = require('express-session')

let RedisStore = require('connect-redis')(session)
let client = redis.createClient()

app.use(
  session({
    store: new RedisStore({ client }),
    secret: 'keyboard cat',
    resave: false,
  })
)

RedisStore(options)

The RedisStore requires an existing Redis client. Any clients compatible with the redis API will work. See client option for more details.

Options

client

An instance of redis or a redis compatible client.

Known compatible and tested clients:

prefix

Key prefix in Redis (default: sess:).

This prefix appends to whatever prefix you may have set on the client itself.

Note: You may need unique prefixes for different applications sharing the same Redis instance. This limits bulk commands exposed in express-session (like length, all, keys, and clear) to a single application's data.

ttl

If the session cookie has a expires date, connect-redis will use it as the TTL.

Otherwise, it will expire the session using the ttl option (default: 86400 seconds or one day).

Note: The TTL is reset every time a user interacts with the server. You can disable this behavior in some instances by using disableTouch.

disableTouch

Disables re-saving and resetting the TTL when using touch (default: false)

The express-session package uses touch to signal to the store that the user has interacted with the session but hasn't changed anything in its data. Typically, this helps keep the users session alive if session changes are infrequent but you may want to disable it to cut down the extra calls or to prevent users from keeping sessions open too long. Also consider enabling if you store a lot of data on the session.

Ref: https://github.com/expressjs/session#storetouchsid-session-callback

serializer

The encoder/decoder to use when storing and retrieving session data from Redis (default: JSON).

interface Serializer {
  parse(string): object
  stringify(object): string
}
scanCount

Value used for count parameter in Redis SCAN command. Used for ids() and all() methods (default: 100).

FAQ

How to a log Redis errors?

client.on('error', console.error)

How do I handle lost connections to Redis?

By default, the redis client will auto-reconnect on lost connections. But requests may come in during that time. In Express, one way you can handle this scenario is including a "session check":

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

Current Tags

  • 4.0.3                                ...           latest (2 months ago)

54 Versions

  • 4.0.3                                ...           2 months ago
  • 4.0.2                                ...           3 months ago
  • 4.0.1                                ...           3 months ago
  • 4.0.0                                ...           3 months ago
  • 3.4.2                                ...           5 months ago
  • 3.4.1                                ...           9 months ago
  • 3.4.0                                ...           a year ago
  • 3.3.3                                ...           2 years ago
  • 3.3.2                                ...           2 years ago
  • 3.3.1                                ...           2 years ago
  • 3.3.0                                ...           3 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.2                                ...           4 years ago
  • 3.0.1                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.5.1                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.1                                ...           4 years ago
  • 2.4.0                                ...           4 years ago
  • 2.3.0                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.0                                ...           6 years ago
  • 1.5.0                                ...           6 years ago
  • 1.4.7                                ...           6 years ago
  • 1.4.6                                ...           6 years ago
  • 1.4.5                                ...           6 years ago
  • 1.4.4                                ...           7 years ago
  • 1.4.3                                ...           7 years ago
  • 1.4.2                                ...           7 years ago
  • 1.4.1                                ...           7 years ago
  • 1.4.0                                ...           7 years ago
  • 1.3.0                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.7                                ...           8 years ago
  • 1.0.6                                ...           8 years ago
  • 1.0.5                                ...           9 years ago
  • 1.0.4                                ...           9 years ago
  • 1.0.3                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.2.3                                ...           9 years ago
  • 0.2.2                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
  • 0.2.1                                ...           9 years ago
  • 0.2.0                                ...           9 years ago
  • 0.1.3                                ...           9 years ago
  • 0.1.2                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.0.2                                ...           9 years ago
  • 0.0.1                                ...           9 years ago
Downloads
Today 182
This Week 1,906
This Month 4,739
Last Day 397
Last Week 2,718
Last Month 11,446
Dependencies (0)
None
Dev Dependencies (11)
Dependents (650)

Copyright 2014 - 2016 © taobao.org |