🌜
🌞
connect-redis

connect-redis

v6.1.3

Redis session store for Connect

npm install connect-redis

README

Build Status npm code-style Downloads

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

Installation

npm:

npm install redis connect-redis express-session

Yarn:

yarn add redis connect-redis express-session

API

const session = require("express-session")
let RedisStore = require("connect-redis")(session)

// [email protected]
const { createClient } = require("redis")
let redisClient = createClient({ legacyMode: true })
redisClient.connect().catch(console.error)

// [email protected]
const { createClient } = require("redis")
let redisClient = createClient()

// ioredis
const Redis = require("ioredis")
let redisClient = new Redis()

app.use(
  session({
    store: new RedisStore({ client: redisClient }),
    saveUninitialized: false,
    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.

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 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

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

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 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.

Release Notes

6.1.3
Published on March 18, 2022
6.1.2
Published on March 14, 2022

Bug fixes

  • #348 - Fix merging bug in 6.1.0 that resulted in sessions being deleted. (@zkldi)

Maintenance

  • #350 - Add keywords in package.json (@noahliechti)
6.1.1
Published on February 7, 2022
  • #347 - Revert concurrent sessions (@wavded)
6.1.0
Published on February 4, 2022

Enhancements

  • #345 - Add support for Redis V4 through legacyMode (@wavded)
  • #333 - Support for concurrent sessions (@xarnze)

Maintenance

  • #344 - Update import format on readme (@cechiorlu)
  • #342 - Add note about using multiple Redis versions (@abhijoshi2k)
6.0.0
Published on June 2, 2021

Breaking changes

  • #327 - Remove Node 10 support (@wavded)

Maintenance

  • #326 - Fix broken windows (@wavded)
5.2.0
Published on April 27, 2021

Enhancements

  • #322 - Delete key if resulting TTL is negative in set method (@baumgarb)

Maintenance

  • #324 - Bump redis from 3.0.2 -> 3.1.2 (@dnwe)
  • #318 - Update readme.md (@lunush)
5.1.0
Published on February 8, 2021

Enhancements

  • #316 - Add disableTTL flag (@xowns4817)

Maintenance

  • #314 - Fix broken windows (@wavded)
5.0.0
By Marc Harter • Published on July 9, 2020
  • Drop support for Node 8
4.0.4
By Marc Harter • Published on January 30, 2020
  • Fix loading of all keys for large databases @patriksimek
4.0.3
By Marc Harter • Published on October 1, 2019
  • Use redis.expire on touch @nathan818fr

General

License
MIT
Typescript Types
Tree-shakeable
No

Popularity

GitHub Stargazers
2,677
Community Interest
3,106
Number of Forks
354

Maintenance

Commits
10/219/2205
Last Commit
Mar 18, 2022
Open Issues
1
Closed Issues
243
Open Pull Requests
0
Closed Pull Requests
48

Versions

Versions Released
10/219/2202
Latest Version Released
Current Tags
latest6.1.3

Contributors

tj
tj
Commits: 96
wavded
wavded
Commits: 77
chirag04
chirag04
Commits: 9
knoxcard
knoxcard
Commits: 7
louischatriot
louischatriot
Commits: 5
knoxcard2
knoxcard2
Commits: 4
garrensmith
garrensmith
Commits: 4
r3wt
r3wt
Commits: 3
undoZen
undoZen
Commits: 2
vodolaz095
vodolaz095
Commits: 2
clement
clement
Commits: 2
jakemitchellxyz
jakemitchellxyz
Commits: 2
lunush
lunush
Commits: 1
kevinsimper
kevinsimper
Commits: 1
nathan818fr
nathan818fr
Commits: 1