Coder Social home page Coder Social logo

connect-redis's Introduction

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").default

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

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

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

connect-redis's People

Contributors

01231 avatar anotherpit avatar bachp avatar barisusakli avatar chirag04 avatar clement avatar garrensmith avatar iamolegga avatar jakemitchellxyz avatar knoxcard avatar knoxcard2 avatar louischatriot avatar masa-shin avatar mikaturunen avatar msamblanet avatar naholyr avatar nathan818fr avatar pasieronen avatar patriksimek avatar r3wt avatar rudfoss avatar slnpacifist avatar strml avatar stuartpb avatar tj avatar toddself avatar undozen avatar vkartk avatar vodolaz095 avatar wavded avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.