Giter VIP home page Giter VIP logo

connect-redis's Introduction

Connect Redis

connect-redis is a Redis session store backed by node_redis, and is insanely fast :). Requires redis >= 2.0.0 for the SETEX command.

Installation

$ npm install connect-redis

A note for Express 3.x users

In order to use the latest connect-redis you also have to use express-session instead of the default connect session middleware.

$ npm install express-session

Then follow the usage instructions below.

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.

  • client An existing client
  • host Redis server hostname
  • port Redis server portno
  • socket Redis server unix_socket

The following additional params may be included:

  • ttl Redis session TTL (expiration) in seconds
  • disableTTL disables setting TTL, keys will stay in redis until evicted by other means (overides ttl)
  • db Database index to use
  • pass Password for Redis authentication
  • prefix Key prefix defaulting to "sess:"
  • unref Set true to unref the Redis client. Warning: this is an experimental feature.

Any options not included in this list will be passed to the redis createClient() method directly.

Usage

Pass the express-session store into connect-redis to create a RedisStore constructor.

var session = require('express-session');
var RedisStore = require('connect-redis')(session);

app.use(session({
    store: new RedisStore(options),
    secret: 'keyboard cat'
}));

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

Can I use a URL scheme to make a connection?

Since node_redis which this library wraps does not include the ability to create a client from a URL. Neither does this library. However, there's a separate module that can be used in conjunction to get this behavior.

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

connect-redis's People

Contributors

tj avatar wavded avatar chirag04 avatar louischatriot avatar garrensmith avatar vodolaz095 avatar clement avatar undozen avatar bmatschullat avatar cwu avatar pixelcort avatar dsiu avatar fredr avatar jerem avatar mekwall avatar mikaturunen avatar bachp avatar stuartpb avatar toddself avatar slnpacifist avatar dead-horse avatar shine-on avatar

Watchers

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