valkeystore

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 10 Imported by: 0

README

valkeystore

GoDoc Run Tests Go Report Card codecov Go Version

A session store backend for gorilla/sessions.

Requirements

Depends on the valkey-go valkey library.

Installation

go get github.com/ikesy/valkeystore

Documentation

Available on godoc.org.

See the repository for full documentation on underlying interface.

Example
package main

import (
  "log"
  "net/http"

  "github.com/ikesy/valkeystore"
  "github.com/gorilla/sessions"
)

func main() {
  // Fetch new store.
  store, err := valkeystore.New([]string{":6379"}, "", "", []byte("secret-key"))
  if err != nil {
    panic(err)
  }
  defer store.Close()

  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // Get a session.
    session, err := store.Get(r, "session-key")
    if err != nil {
      log.Println(err.Error())
      return
    }

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

    // Save.
    if err = sessions.Save(r, w); err != nil {
      log.Fatalf("Error saving session: %v", err)
    }

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

  log.Fatal(http.ListenAndServe(":8080", nil))
}

Configuration

SetKeyPrefix

Sets the key prefix for session keys in valkey.

store.SetKeyPrefix("myprefix-")
SetMaxAge

Sets the maximum age, in seconds, of the session record both in the database and in the browser.

store.SetMaxAge(86400 * 7) // 7 days

License

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

Documentation

Overview

Package valkeystore is a session store backend for gorilla/sessions

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ValkeyStore

type ValkeyStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options // default configuration
	// contains filtered or unexported fields
}

ValkeyStore represents a valkey session store.

Example
// valkeystore
store, err := New([]string{":6379"}, "", "", []byte("secret-key"))
if err != nil {
	panic(err)
}
defer store.Close()

func New added in v0.2.0

func New(address []string, username, password string, keyPairs ...[]byte) (*ValkeyStore, error)

New creates a new valkey store with the given parameters and key pairs.

func NewWithClient added in v0.2.0

func NewWithClient(client valkey.Client, keyPairs ...[]byte) (*ValkeyStore, error)

NewWithClient creates a new valkey store with the given client and key pairs.

func NewWithDatabase added in v0.2.0

func NewWithDatabase(
	address []string,
	username,
	password string,
	database int,
	keyPairs ...[]byte,
) (*ValkeyStore, error)

NewWithDatabase creates a new valkey store with the given parameters and key pairs.

func NewWithURL added in v0.2.0

func NewWithURL(url string, keyPairs ...[]byte) (*ValkeyStore, error)

NewWithURL creates a new valkey store with the given URL and key pairs.

func (*ValkeyStore) Close

func (r *ValkeyStore) Close()

Close closes the valkey client connection.

func (*ValkeyStore) Get

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

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

func (*ValkeyStore) New

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

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

func (*ValkeyStore) Save

func (r *ValkeyStore) Save(request *http.Request, writer http.ResponseWriter, session *sessions.Session) error

Save adds a single session to the response.

func (*ValkeyStore) SetKeyPrefix

func (r *ValkeyStore) SetKeyPrefix(keyPrefix string)

SetKeyPrefix set the prefix.

func (*ValkeyStore) SetMaxAge

func (r *ValkeyStore) SetMaxAge(maxAge int)

SetMaxAge 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 `age` 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 encrypting algorithm you should use this function to change `MaxAge` value.

Jump to

Keyboard shortcuts

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