gorillaradix

package module
v0.0.0-...-393e1d4 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2020 License: MIT Imports: 13 Imported by: 0

README

gorillaradix

gorillaradix is a gorilla sessions.Store using radix as its persistence layer.

It supports vanilla redis and redis cluster

Quickstart

To use this library you'd need to do something like:

Vanilla redis
store, err := gorillaradix.NewStore(gorillaradix.PoolConfiguration{
	Host: "my-redis-host:6379",
	ConnectionOptions: gorillaradix.ConnectionOptions{
		Password:    "terriblePassword",
		PingTimeout: 10 * time.Second,
		Timeout:     1 * time.Minute,
	    },
	}, gorillaradix.SessionOptions{
	Secret:      "terribleSecret",
})
redis cluster
store, err := gorillaradix.NewStoreCluster(gorillaradix.PoolConfiguration{
	Hosts: []string{"my-cluster-host-1:6379", "my-cluster-host-2:6379"},
	ConnectionOptions: gorillaradix.ConnectionOptions{
		Password:    "terriblePassword",
		PingTimeout: 10 * time.Second,
		Timeout:     1 * time.Minute,
	    },
	}, gorillaradix.SessionOptions{
	Secret:      "terribleSecret",
})

Options

While there are reasonable defaults provided by the library, there are a number of variables to customize this session store.

Notable session options:
Variable Description Default
KeyPrefix Start session keys in redis with this string session_
MaxAge How long session data should be retained in seconds 86400
MaxLength How much data should a session be able to store in bytes 32768
Path What path should the cookie be set to /
Secret Secret (as a string) to validate session cookies notAGreatSecret

Dependencies

  • github.com/gorilla/sessions
  • github.com/gorilla/securecookie
  • github.com/mediocregopher/radix

FAQ

Why use the radix library?

It is one of the two client libraries recommended by the redis project

Documentation

Index

Constants

View Source
const (
	ErrNonStringKeyValue              = "non-string key value, cannot serialize session to JSON"
	ErrJSONSerializerSerializeError   = "gorillaradix.JSONSerializer.Serialize()"
	ErrJSONSerializerDeSerializeError = "gorillaradix.JSONSerializer.Deserialize()"
)
View Source
const (
	ErrSessionDataTooBig = "gorillaradix.RadixStore.save: data to persist is over size limit"
)

Amount of time for cookies/redis keys to expire.

Variables

This section is empty.

Functions

func NewStore

func NewStore(configuration PoolConfiguration, options SessionOptions) (sessions.Store, error)

func NewStoreCluster

func NewStoreCluster(configuration ClusterConfiguration, options SessionOptions) (sessions.Store, error)

Types

type ClusterConfiguration

type ClusterConfiguration struct {
	ConnectionOptions
	Hosts []string
}

Redis Cluster specific options

type ConnectionOptions

type ConnectionOptions struct {
	Password    string        // Password for the redis server
	PingTimeout time.Duration // How often should we ping the redis instance
	Timeout     time.Duration // Timeout on connecting
}

Universal Redis Options

type GobSerializer

type GobSerializer struct{}

GobSerializer uses 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 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 PoolConfiguration

type PoolConfiguration struct {
	ConnectionOptions
	Host string
}

Vanilla Redis specific options

type RadixStore

type RadixStore struct {
	Codecs  []securecookie.Codec
	Options SessionOptions
	// contains filtered or unexported fields
}

func (*RadixStore) Get

func (rs *RadixStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get should return a cached session.

func (*RadixStore) New

func (rs *RadixStore) New(r *http.Request, name string) (*sessions.Session, error)

New creates new session and if possible, loads the stored data into it

func (*RadixStore) Save

Save should persist session to redis

type SerializerError

type SerializerError struct {
	Err      error
	Location string
}

func (*SerializerError) Error

func (err *SerializerError) Error() string

type SessionOptions

type SessionOptions struct {
	sessions.Options
	MaxLength int    // Max amount of storage to be allowed in session data for a given user
	KeyPrefix string // within redis, what should session data keys be prefixed with
	Secret    string // Secret is for validating a session key
}

type SessionSerializer

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

SessionSerializer provides an interface hook for alternative serializers

Jump to

Keyboard shortcuts

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