npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

connect-redis-session

v1.0.6

Published

Redis session store for Express

Downloads

2

Readme

connect-redis-session

Redis session storage for Express supporting the latest node-redis client.

npm codecov github-workflow


Features:

  • Promise-based methods for direct interaction with the sessions store
  • Atomic single-key operations (get, set, touch, destroy)
  • Batched multi-key operations (all, length, clear) for efficient performance
  • Safeguards for handling race conditions caused by concurrent requests
  • First class support for Typescript

Compatibility:


Installation

npm install connect-redis-session # redis@^4 express-session@^1.17
yarn add connect-redis-session # redis@^4 express-session@^1.17 

Usage

Quick Start

const session = require('express-session');
const redis = require('redis');
const { RedisStore } = require('connect-redis-session');

// Create the Redis client
const client = redis.createClient();

// Configure the Redis store
const store = new RedisStore({ client });

// Configure the Express session middleware
app.use(
    session({
        store,
        secret: 'swordfish',
        saveUninitialized: false, // recommended
        resave: false, // recommended
        // ...
    }),
);

Access with Promises

The RedisStore.access field exposes methods for directly interacting with the store using Promises.

const updateSession = async (sid) => {
    // Get a session from the store
    const session = await store.access.get(sid);

    // Create or update a session
    await store.access.set(sid, { ...session, foo: 'bar' })

    // Delete a session
    await store.access.destroy(sid);

    // Get all sessions
    const sessions = await session.access.all();

    // Count all sessions
    const n = await session.access.length();

    // Clear all session keys from the store
    await store.access.clear();
}

Options

const store = new RedisStore({
    client,
    prefix: 'sessions:',
    scanCount: 100,
    ttlSeconds: 86400,
    concurrencyGraceSeconds: 300,
    disableTouch: false,
})

client

object | required

An initialized node-redis v4 client.

Prior to server listening, the client's connect method should be called.

(async () => {
    await client.connect();
    server.listen(80);
})();

prefix

string • 'sessions:'

A prefix used for each key in the session store.


scanCount

number • 100

The maximum number of keys batched in Redis SCAN calls. This also helps limit the memory load on subsequent calls using the key batches (e.g. MGET, DEL).


ttlSeconds

number | false86400 1 day

The fallback duration in seconds after which a created or updated session should be expired.

This field is only used when a session is missing the cookie.expires field.

When set to 0 or false, the store will reject sessions missing the cookie.expires field.


concurrencyGraceSeconds

number • 300

The duration in seconds after tombstone records are removed from the store.

Tombstone records are used to prevent a destroyed session from being updated or touched. This lock is retained for the duration specified by this setting.


disableTouch

boolean • false

Disables renewing the session's time to live when the session's touch method is used.

Setting this option to true is not recommended and should share the same value as the session's resave option.


serializer

object

A custom serializer implementing the following encoding and decoding methods for storing session data as Redis string values:

  • stringify: (value: SessionData) => string
  • parse: (text: string) => SessionData

Refer to the global JSON object for an example.


License

MIT License