redisstore

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2020 License: MIT Imports: 12 Imported by: 0

README

RedisStore

A Gorilla Sessions Store implementation backed by Redis.

It uses go-redis as client to connect to Redis.

Example


package main

import (
    "github.com/go-redis/redis"
    "github.com/gorilla/sessions"
    "github.com/rbcervilla/redisstore"
    "log"
    "net/http"
    "net/http/httptest"
)

func main() {

    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
    })

    // New default RedisStore
    store, err := redisstore.NewRedisStore(client)
    if err != nil {
        log.Fatal("failed to create redis store: ", err)
    }

    // Example changing configuration for sessions
    store.KeyPrefix("session_")
    store.Options(sessions.Options{
        Path:   "/path",
        Domain: "example.com",
        MaxAge: 86400 * 60,
    })

    // Request y writer for testing
    req, _ := http.NewRequest("GET", "http://www.example.com", nil)
    w := httptest.NewRecorder()

    // Get session
    session, err := store.Get(req, "session-key")
    if err != nil {
        log.Fatal("failed getting session: ", err)
    }

    // Add a value
    session.Values["foo"] = "bar"

    // Save session
    if err = sessions.Save(req, w); err != nil {
        log.Fatal("failed saving session: ", err)
    }

    // Delete session (MaxAge <= 0)
    session.Options.MaxAge = -1
    if err = sessions.Save(req, w); err != nil {
        log.Fatal("failed deleting session: ", err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GobSerializer

type GobSerializer struct{}

Gob serializer

func (GobSerializer) Deserialize

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

func (GobSerializer) Serialize

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

type KeyGenFunc

type KeyGenFunc func() (string, error)

KeyGenFunc defines a function used by store to generate a key

type RedisStore

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

RedisStore stores gorilla sessions in Redis

func NewRedisStore

func NewRedisStore(client redis.UniversalClient) (*RedisStore, error)

NewRedisStore returns a new RedisStore with default configuration

func (*RedisStore) Get

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

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

func (*RedisStore) KeyGen

func (s *RedisStore) KeyGen(f KeyGenFunc)

KeyGen sets the key generator function

func (*RedisStore) KeyPrefix

func (s *RedisStore) KeyPrefix(keyPrefix string)

KeyPrefix sets the key prefix to store session in Redis

func (*RedisStore) New

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

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

func (*RedisStore) Options

func (s *RedisStore) Options(opts sessions.Options)

Options set options to use when a new session is created

func (*RedisStore) Save

func (s *RedisStore) 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 then the session file will be deleted from the store. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.

func (*RedisStore) Serializer

func (s *RedisStore) Serializer(ss SessionSerializer)

Serializer sets the session serializer to store session

func (*RedisStore) WithContext

func (s *RedisStore) WithContext(context context.Context) *RedisStore

type SessionSerializer

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

SessionSerializer provides an interface for serialize/deserialize a session

Jump to

Keyboard shortcuts

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