You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/alexedwards/scs/redisstore

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/alexedwards/scs/redisstore

v0.0.0-20250417082927-ab20b3feb5e9
Source
Go
Version published
Created
Source

redisstore

A Redis based session store for SCS.

Setup

You should follow the instructions to setup a connection pool, and pass the pool to redisstore.New() to establish the session store.

Example

package main

import (
	"io"
	"net/http"

	"github.com/alexedwards/scs/v2"
	"github.com/alexedwards/scs/redisstore"
	"github.com/gomodule/redigo/redis"
)

var sessionManager *scs.SessionManager

func main() {
	// Establish connection pool to Redis.
	pool := &redis.Pool{
		MaxIdle: 10,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", "host:6379")
		},
	}
	
	// Initialize a new session manager and configure it to use redisstore as the session store.
	sessionManager = scs.New()
	sessionManager.Store = redisstore.New(pool)

	mux := http.NewServeMux()
	mux.HandleFunc("/put", putHandler)
	mux.HandleFunc("/get", getHandler)

	http.ListenAndServe(":4000", sessionManager.LoadAndSave(mux))
}

func putHandler(w http.ResponseWriter, r *http.Request) {
	sessionManager.Put(r.Context(), "message", "Hello from a session!")
}

func getHandler(w http.ResponseWriter, r *http.Request) {
	msg := sessionManager.GetString(r.Context(), "message")
	io.WriteString(w, msg)
}

Expired Session Cleanup

Redis will automatically remove expired session keys.

Key Collisions

By default keys are in the form scs:session:<token>. For example:

"scs:session:ZnirGwi2FiLwXeVlP5nD77IpfJZMVr6un9oZu2qtJrg"

Because the token is highly unique, key collisions are not a concern. But if you're configuring multiple session managers, both of which use redisstore, then you may want the keys to have a different prefix depending on which session manager wrote them. You can do this by using the NewWithPrefix() method like so:

pool := &redis.Pool{
    MaxIdle: 10,
    Dial: func() (redis.Conn, error) {
        return redis.Dial("tcp", "host:6379")
    },
}

sessionManagerOne = scs.New()
sessionManagerOne.Store = redisstore.NewWithPrefix(pool, "scs:session:1:")

sessionManagerTwo = scs.New()
sessionManagerTwo.Store = redisstore.NewWithPrefix(pool, "scs:session:2:")

Iterating over all Sessions

If you intend to use the sessionstore.Iterate() function to iterate over all sessions on a busy Redis server with many keys stored, be warned that this can take a long time and is therefore probably only interesting for debugging purposes.

FAQs

Package last updated on 17 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.