valkeystore

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 13 Imported by: 0

README

Testing

gorilla/sessions Store implementation for valkey

This implementation uses valkey-io/go-valkey as the client.

Usage

go get github.com/JensvandeWiel/valkeystore
package main

import (
	"github.com/JensvandeWiel/valkeystore"
	"github.com/gorilla/sessions"
	"github.com/valkey-io/valkey-go"
)

func main() {
	client, err := valkey.NewClient(valkey.ClientOption{
		InitAddress: []string{"localhost:6379"},
	})
	if err != nil {
		t.Fatal("failed to create valkey client", err)
	}

	defer client.Close()

	store, err := valkeystore.NewValkeyStore(client)
	if err != nil {
		t.Fatal("failed to create redis store", err)
	}
}

For more examples check the tests or godoc

License

This project is licensed under the MIT License - see the LICENSE file for details

Contributing

Feel free to open a pull request.

This project is inspired by:

Documentation

Index

Constants

View Source
const DefaultKeyPrefix = "session:"

Variables

This section is empty.

Functions

This section is empty.

Types

type GobSerializer

type GobSerializer struct{}

GobSerializer is a Serializer that uses gob encoding

func NewGobSerializer

func NewGobSerializer() *GobSerializer

NewGobSerializer creates a new GobSerializer

func (*GobSerializer) Deserialize

func (s *GobSerializer) Deserialize(b []byte, session *sessions.Session) error

Deserialize deserializes a session using gob encoding

func (*GobSerializer) Serialize

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

Serialize serializes a session using gob encoding

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer is a Serializer that uses JSON encoding

func NewJSONSerializer

func NewJSONSerializer() *JSONSerializer

NewJSONSerializer creates a new JSONSerializer

func (*JSONSerializer) Deserialize

func (s *JSONSerializer) Deserialize(b []byte, session *sessions.Session) error

Deserialize deserializes a session using JSON encoding

func (*JSONSerializer) Serialize

func (s *JSONSerializer) Serialize(session *sessions.Session) ([]byte, error)

Serialize serializes a session using JSON encoding

type KeyGenFunc

type KeyGenFunc func() (string, error)

KeyGenFunc is a function that generates a new session key

type OptionsFunc

type OptionsFunc = func(*ValkeyStore)

OptionsFunc is a function that sets options on a ValkeyStore

func WithKeyGenFunc

func WithKeyGenFunc(kg KeyGenFunc) OptionsFunc

WithKeyGenFunc sets the key generation function for a ValkeyStore

func WithKeyPrefix

func WithKeyPrefix(p string) OptionsFunc

WithKeyPrefix sets the key prefix for a ValkeyStore

func WithSerializer

func WithSerializer(s Serializer) OptionsFunc

WithSerializer sets the serializer for a ValkeyStore

func WithSessionOptions

func WithSessionOptions(options sessions.Options) OptionsFunc

WithSessionOptions sets the default session options for a ValkeyStore

type Serializer

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

Serializer is an interface for serializing and deserializing sessions for Valkey

type ValkeyStore

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

ValkeyStore stores gorilla sessions in Valkey

func NewValkeyStore

func NewValkeyStore(client valkey.Client, options ...OptionsFunc) (*ValkeyStore, error)

NewValkeyStore creates a new ValkeyStore with the given client and options.

func (*ValkeyStore) Close

func (s *ValkeyStore) Close()

Close closes the store

func (*ValkeyStore) Get

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

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

func (*ValkeyStore) KeyGen

func (s *ValkeyStore) KeyGen(keyGen KeyGenFunc)

KeyGen sets the key generator for the store.

func (*ValkeyStore) KeyPrefix

func (s *ValkeyStore) KeyPrefix(keyPrefix string)

KeyPrefix sets the key prefix for the store.

func (*ValkeyStore) New

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

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

func (*ValkeyStore) Options

func (s *ValkeyStore) Options(options sessions.Options)

Options sets the default session options for the store.

func (*ValkeyStore) Save

func (s *ValkeyStore) 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 (*ValkeyStore) Serializer

func (s *ValkeyStore) Serializer(ss Serializer)

Serializer sets the serializer for the store.

Jump to

Keyboard shortcuts

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