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 string)

KeyPrefix sets the key prefix to store session in Redis

func (*RedisStore) MaxAge

func (s *RedisStore) MaxAge(v int)

MaxAge restricts the maximum age, in seconds, of the session record both in database and a browser. This is to change session storage configuration. If you want just to remove session use your session `s` object and change it's `Options.MaxAge` to -1, as specified in

http://godoc.org/github.com/gorilla/sessions#Options

Default is the one provided by this package value - `sessionExpire`. Set it to 0 for no restriction. Because we use `MaxAge` also in SecureCookie crypting algorithm you should use this function to change `MaxAge` value.

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) SetMaxLength

func (s *RedisStore) SetMaxLength(l int)

SetMaxLength sets RediStore.maxLength if the `l` argument is greater or equal 0 maxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new RediStore is 4096. Redis allows for max. value sizes of up to 512MB (http://redis.io/topics/data-types) Default: 4096,

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