redisstore

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 10 Imported by: 0

README

redisstore

Go Report Card codecov Tests GoDoc

A gorilla/sessions store implementation with Redis. I was frustrated with the available packages since they are either outdated or too closely linked with a Redis client package. So, I made my own package that is adaptable, modern and well tested.

Install

go get github.com/joelrose/redisstore

Example

package main

import (
	"net/http"

	"github.com/gorilla/sessions"
	"github.com/joelrose/redisstore"
	"github.com/joelrose/redisstore/adapter"
	goredis "github.com/redis/go-redis/v9"
    redigo "github.com/gomodule/redigo/redis"
)

func main() {
	// Create a new go-redis client, pool or cluster
	goRedisClient := goredis.NewClient(&goredis.Options{
		Addr: "localhost:6379",
	})

	// Create a new redigo pool
	// redigoPool := &redigo.Pool{
	// 	Dial: func() (redigo.Conn, error) {
	// 		return redigo.Dial("tcp", "localhost:6379")
	// 	},
	// }

	// New Store
	keys := [][]byte{[]byte("hash")}
	store := redisstore.New(
		adapter.UseGoRedis(goRedisClient),
		// adapter.UseRedigo(redigoPool),
		keys,
		redisstore.WithSessionOptions(sessions.Options{
			Path:   "/",
			Domain: "example.com",
			MaxAge: 86400 * 30,
		}),
		redisstore.WithKeyPrefix("prefix_"),
	)

	// Get Session from a http.Request
	var req *http.Request
	session, err := store.Get(req, "session-name")

	// Add your data to the session
	session.Values["foo"] = "bar"

	// Save session to http.ResponseWriter
	var w http.ResponseWriter
	err = sessions.Save(req, w)

	// Delete session (MaxAge <= 0) and save to http.ResponseWriter
	session.Options.MaxAge = -1
	err = sessions.Save(req, w)
}

License

This project is licensed under the MIT license. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v1.1.0

type Client interface {
	// Get returns the value for a given key.
	Get(ctx context.Context, key string) ([]byte, error)
	// Set sets the value for a given key.
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	// Del deletes a given key.
	Del(ctx context.Context, key string) error
}

type GobSerializer

type GobSerializer struct{}

GobSerializer uses the gob package to encode the session map.

func (GobSerializer) Deserialize

func (s GobSerializer) Deserialize(d []byte, ss *sessions.Session) error

Deserialize back to map[interface{}]interface{}.

func (GobSerializer) Serialize

func (s GobSerializer) Serialize(ss *sessions.Session) ([]byte, error)

Serialize using gob.

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer encodes the session map to JSON.

func (JSONSerializer) Deserialize

func (s JSONSerializer) Deserialize(d []byte, ss *sessions.Session) error

Deserialize back to map[string]interface{}.

func (JSONSerializer) Serialize

func (s JSONSerializer) Serialize(ss *sessions.Session) ([]byte, error)

Serialize to JSON. All keys must be strings.

type KeyGenFunc

type KeyGenFunc func() string

KeyGenFunc defines a function used by store to generate the session key.

type Options

type Options func(s *Store)

func WithKeyGenerator

func WithKeyGenerator(keyGen KeyGenFunc) Options

WithKeyGenerator sets the key generator used to generate the session key. By default, the defaultKeyGenerator method is used.

func WithKeyPrefix

func WithKeyPrefix(prefix string) Options

WithKeyPrefix sets the key prefix used in redis keys.

func WithSerializer

func WithSerializer(serializer SessionSerializer) Options

WithSerializer sets the serializer used to serialize the session. By default, the GobSerializer is used.

func WithSessionOptions

func WithSessionOptions(options sessions.Options) Options

WithSessionOptions sets the session options.

type SessionSerializer

type SessionSerializer interface {
	Deserialize(d []byte, ss *sessions.Session) error
	Serialize(ss *sessions.Session) ([]byte, error)
}

SessionSerializer provides an interface for alternative serializers.

type Store

type Store struct {
	Options *sessions.Options
	Codecs  []securecookie.Codec
	// contains filtered or unexported fields
}

func New

func New(client Client, keyPairs [][]byte, options ...Options) *Store

func (*Store) Get

func (s *Store) Get(r *http.Request, name string) (*sessions.Session, error)

Get returns a session for the given name after adding it to the registry.

ref: https://github.com/gorilla/sessions/blob/0e1d1d7c382124033b710ef1ef0993327195ed40/store.go#L178

func (*Store) New

func (s *Store) New(r *http.Request, name string) (*sessions.Session, error)

New returns a session for the given name without adding it to the registry.

ref: https://github.com/gorilla/sessions/blob/0e1d1d7c382124033b710ef1ef0993327195ed40/store.go#L185

func (*Store) Save

func (s *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save adds a single session to the response.

If the Options.MaxAge of the session is <= 0, the session is deleted.

func (*Store) SetMaxAge added in v1.0.2

func (s *Store) SetMaxAge(age int)

SetMaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

ref: https://github.com/gorilla/sessions/blob/0e1d1d7c382124033b710ef1ef0993327195ed40/store.go#L243

func (*Store) SetOptions added in v1.0.2

func (s *Store) SetOptions(options sessions.Options)

SetOptions sets the options for the store.

Directories

Path Synopsis
nolint: wrapcheck
nolint: wrapcheck
Package redisstore is a generated GoMock package.
Package redisstore is a generated GoMock package.

Jump to

Keyboard shortcuts

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