goredisstore

package module
v0.0.0-...-7e11d57 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 3 Imported by: 3

README

goredisstore

A Redis based session store for SCS.

Setup

You should follow the instructions to setup a client, and pass the client to goredisstore.New() to establish the session store.

Example

package main

import (
	"io"
	"net/http"

	"github.com/alexedwards/scs/v2"
	"github.com/alexedwards/scs/goredisstore"
	"github.com/redis/go-redis/v9"
)

var sessionManager *scs.SessionManager

func main() {
	// Establish connection pool to Redis.
	opt, err := redis.ParseURL("redis://localhost:6379")
	if err != nil {
		panic(err)
	}
	client := redis.NewClient(opt)
	defer client.Close()

	// Initialize a new session manager and configure it to use goredisstore as the session store.
	sessionManager = scs.New()
	sessionManager.Store = goredisstore.New(client)

	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 goredisstore, 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:

opt, err := redis.ParseURL("redis://localhost:6379")
if err != nil {
    panic(err)
}
client := redis.NewClient(opt)

sessionManagerOne = scs.New()
sessionManagerOne.Store = goredisstore.NewWithPrefix(client, "scs:session:1:")

sessionManagerTwo = scs.New()
sessionManagerTwo.Store = goredisstore.NewWithPrefix(client, "scs:session:2:")

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(client *redis.Client) *RedisStore

New returns a new RedisStore instance. The client parameter should be a pointer to a go-redis connection.

func NewWithPrefix

func NewWithPrefix(client *redis.Client, 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) AllCtx

func (r *RedisStore) AllCtx(ctx context.Context) (map[string][]byte, error)

AllCtx 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

func (*RedisStore) CommitCtx

func (r *RedisStore) CommitCtx(ctx context.Context, token string, b []byte, expiry time.Time) error

CommitCtx 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

func (*RedisStore) DeleteCtx

func (r *RedisStore) DeleteCtx(ctx context.Context, token string) error

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

func (*RedisStore) Find

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

func (*RedisStore) FindCtx

func (r *RedisStore) FindCtx(ctx context.Context, token string) (b []byte, exists bool, err error)

FindCtx 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