connect-redis
TypeScript icon, indicating that this package has built-in type declarations

8.0.1 • Public • Published

Build Status npm code-style Downloads

connect-redis provides Redis session storage for Express.

Installation

connect-redis requires express-session to installed and one of the following compatible Redis clients:

Install with redis:

npm install redis connect-redis express-session

Install with ioredis:

npm install ioredis connect-redis express-session

Importing

connect-redis supports both CommonJS (require) and ESM (import) modules.

Import using ESM/Typescript:

import {RedisStore} from "connect-redis"

Require using CommonJS:

const {RedisStore} = require("connect-redis")

API

Full setup using redis package:

import {RedisStore} from "connect-redis"
import session from "express-session"
import {createClient} from "redis"

// Initialize client.
let redisClient = createClient()
redisClient.connect().catch(console.error)

// Initialize store.
let redisStore = new RedisStore({
  client: redisClient,
  prefix: "myapp:",
})

// Initialize session storage.
app.use(
  session({
    store: redisStore,
    resave: false, // required: force lightweight session keep alive (touch)
    saveUninitialized: false, // recommended: only save session when data exists
    secret: "keyboard cat",
  }),
)

RedisStore(options)

Options

client

An instance of redis or ioredis.

prefix

Key prefix in Redis (default: sess:).

Note: 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).

interface RedisStoreOptions {
  ...
  ttl?: number | {(sess: SessionData): number}
}

ttl also has external callback support. You can use it for dynamic TTL generation. It has access to session data.

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

Note: express-session does not update expires until the end of the request life cycle. Calling session.save() manually beforehand will have the previous value.

disableTouch

Disables 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

disableTTL

Disables key expiration completely (default: false)

This option disables key expiration requiring the user to manually manage key cleanup outside of connect-redis. Only use if you know what you are doing and have an exceptional case where you need to manage your own expiration in Redis.

Note: This has no effect on express-session setting cookie expiration.

serializer

Provide a custom encoder/decoder to use when storing and retrieving session data from Redis (default: JSON.parse and JSON.stringify).

Optionally parse method can be async if need be.

interface Serializer {
  parse(string): object | Promise<object>
  stringify(object): string
}
scanCount

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
8.0.123,116latest
8.0.1-rc.28next

Version History

VersionDownloads (Last 7 Days)Published
8.0.123,116
8.0.1-rc.28
8.0.1-rc.19
8.0.03,208
8.0.0-rc.12
7.1.191,387
7.1.023,300
7.0.13,625
7.0.0608
7.0.0-rc.22
7.0.0-rc.11
6.1.348,417
6.1.12,331
6.0.09,606
5.2.0162,267
5.1.02,118
5.0.02,037
4.0.410,331
4.0.317,337
4.0.2153
4.0.13
4.0.026
3.4.217,847
3.4.11,005
3.4.0873
3.3.31,416
3.3.2209
3.3.11
3.3.0407
3.2.0213
3.1.0325
3.0.2196
3.0.144
3.0.033
2.5.1206
2.5.09
2.4.1172
2.4.04
2.3.072
2.2.0126
2.1.02,800
2.0.073
1.5.076
1.4.796
1.4.66
1.4.5241
1.4.411
1.4.36
1.4.24
1.4.14
1.4.05
1.3.09
1.2.04
1.1.012
1.0.76
1.0.612
1.0.55
1.0.46
1.0.35
1.0.25
1.0.14
1.0.04
0.2.312
0.2.217
0.1.08
0.2.05
0.1.15
0.1.312
0.2.15
0.1.220
0.0.14
0.0.227

Package Sidebar

Install

npm i connect-redis

Weekly Downloads

426,663

Version

8.0.1

License

MIT

Unpacked Size

33.2 kB

Total Files

17

Last publish

Collaborators

  • tjholowaychuk
  • chirag04
  • wavded