redisstore

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: MIT Imports: 12 Imported by: 0

README

Build Status Maintainability Test Coverage Go Report Card

RedisStore

Install

go get -u github.com/efureev/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/v7"
    "github.com/gorilla/sessions"
    "github.com/efureev/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{}

GobSerializer is Gob Serializer

func (GobSerializer) Deserialize

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

Deserialize back to map[string]interface{}

func (GobSerializer) Serialize

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

Serialize to []byte

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer encode 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. Will err if there are unmarshalable key values

type KeyGenFunc

type KeyGenFunc func() string

KeyGenFunc defines a function used by store to generate a key

type RedisStore

type RedisStore struct {
	Codecs []securecookie.Codec

	DefaultMaxAge int // default Redis TTL for a MaxAge == 0 session
	// contains filtered or unexported fields
}

RedisStore stores gorilla sessions in Redis

func NewRedisStore

func NewRedisStore(client redis.UniversalClient, keyPairs ...[]byte) (*RedisStore, error)

NewRedisStore returns a new RedisStore with default configuration

func NewRedisStoreSimple

func NewRedisStoreSimple(address, password string, db int, keyPairs ...[]byte) (*RedisStore, error)

NewRedisStoreSimple returns a new RedisStore with easy config

func NewRedisStoreWithRedisConfig

func NewRedisStoreWithRedisConfig(options *redis.Options, keyPairs ...[]byte) (*RedisStore, error)

NewRedisStoreWithRedisConfig returns a new RedisStore with default Redis configuration

func (*RedisStore) Close

func (s *RedisStore) Close() error

Close closes the underlying *redis client

func (*RedisStore) Delete added in v1.1.0

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

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