redisstore

package module
v0.0.0-...-ab20b3f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 2 Imported by: 131

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

RedisStore represents the session store.

func New

func New(pool *redis.Pool) *RedisStore

New returns a new RedisStore instance. The pool parameter should be a pointer to a redigo connection pool. See https://godoc.org/github.com/gomodule/redigo/redis#Pool.

func NewWithPrefix

func NewWithPrefix(pool *redis.Pool, prefix string) *RedisStore

NewWithPrefix returns a new RedisStore instance. The pool parameter should be a pointer to a redigo connection pool. The prefix parameter controls the Redis key prefix, which can be used to avoid naming clashes if necessary.

func (*RedisStore) All

func (r *RedisStore) All() (map[string][]byte, error)

All returns a map containing the token and data for all active (i.e. not expired) sessions in the RedisStore instance.

func (*RedisStore) Commit

func (r *RedisStore) Commit(token string, b []byte, expiry time.Time) error

Commit adds a session token and data to the RedisStore instance with the given expiry time. If the session token already exists then the data and expiry time are updated.

func (*RedisStore) Delete

func (r *RedisStore) Delete(token string) error

Delete removes a session token and corresponding data from the RedisStore instance.

func (*RedisStore) Find

func (r *RedisStore) Find(token string) (b []byte, exists bool, err error)

Find returns the data for a given session token from the RedisStore instance. If the session token is not found or is expired, the returned exists flag will be set to false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL