redistore

package module
v0.0.0-...-008b2c2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 9 Imported by: 0

README

redistore

GoDoc Build Status

A session store backend for gorilla/sessions - src.

Requirements

Depends on the go-redis/redis Redis library.

Installation

go get gopkg.in/boj/redistore.v1

Documentation

Available on godoc.org.

See http://www.gorillatoolkit.org/pkg/sessions for full documentation on underlying interface.

Example
// Fetch new store.
store, err := NewRediStore(10, "tcp", ":6379", "", []byte("secret-key"))
if err != nil {
	panic(err)
}
defer store.Close()

// Get a session.
session, err = store.Get(req, "session-key")
if err != nil {
	log.Error(err.Error())
}

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

// Save.
if err = sessions.Save(req, rsp); err != nil {
	t.Fatalf("Error saving session: %v", err)
}

// Delete session.
session.Options.MaxAge = -1
if err = sessions.Save(req, rsp); err != nil {
	t.Fatalf("Error saving session: %v", err)
}

Documentation

Overview

Package redistore is a session store backend for gorilla/sessions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 RediStore

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

func NewRedisStore

func NewRedisStore(client redis.UniversalClient, keyPrefix string, keyPairs ...[]byte) (*RediStore, error)

NewRedisStore returns a new RedisStore.

func (*RediStore) Client

func (s *RediStore) Client() redis.UniversalClient

Client returns the Client.

func (*RediStore) Delete

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

Delete removes the session from redis, and sets the cookie to expire.

func (*RediStore) DeleteByID

func (s *RediStore) DeleteByID(ids ...string) error

DeleteByID deletes sessions from redis by id.

func (*RediStore) Get

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

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

See gorilla/sessions FilesystemStore.Get().

func (*RediStore) GetAll

func (s *RediStore) GetAll() ([]*sessions.Session, error)

GetAll returns all sessions stored in redis.

func (*RediStore) KeyPrefix

func (s *RediStore) KeyPrefix() string

Client returns the prefix.

func (*RediStore) MaxLength

func (s *RediStore) MaxLength() int

Client returns the *sessions.Options.

func (*RediStore) New

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

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

See gorilla/sessions FilesystemStore.New().

func (*RediStore) Options

func (s *RediStore) Options() *sessions.Options

Client returns the *sessions.Options.

func (*RediStore) Save

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

Save adds a single session to the response.

func (*RediStore) SetKeyPrefix

func (s *RediStore) SetKeyPrefix(p string) *RediStore

SetKeyPrefix set the prefix

func (*RediStore) SetMaxLength

func (s *RediStore) SetMaxLength(length int) *RediStore

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,

func (*RediStore) SetOptions

func (s *RediStore) SetOptions(opts *sessions.Options) *RediStore

SetOptions set the session options

func (*RediStore) SetSerializer

func (s *RediStore) SetSerializer(serializer SessionSerializer) *RediStore

SetSerializer sets the serializer.

func (*RediStore) Update

func (s *RediStore) Update(session *sessions.Session) error

Update updates the session in redis.

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