badgerstore

package module
v0.0.0-...-7e11d57 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 2 Imported by: 3

README

badgerstore

A Badger based session store for SCS.

Setup

You should follow the instructions to install and open a database, and pass the database to badgerstore.New() to establish the session store.

Example

package main

import (
	"io"
	"net/http"

	"github.com/alexedwards/scs/v2"
	"github.com/alexedwards/scs/badgerstore"
	"github.com/dgraph-io/badger"
)

var sessionManager *scs.SessionManager

func main() {
	// Open a Badger database.
	db, err := badger.Open(badger.DefaultOptions("tmp/badger"))
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Initialize a new session manager and configure it to use badgerstore as the session store.
	sessionManager = scs.New()
	sessionManager.Store = badgerstore.New(db)

	mux := http.NewServeMux()
	mux.HandleFunc("/put", putHandler)
	mux.HandleFunc("/get", getHandler)

	http.ListenAndServe(":4000", sessionManager.LoadAndSave(mux))
}

func putHandler(w http.ResponseWriter, r *http.Request) {
	sessionManager.Put(r.Context(), "message", "Hello from a session!")
}

func getHandler(w http.ResponseWriter, r *http.Request) {
	msg := sessionManager.GetString(r.Context(), "message")
	io.WriteString(w, msg)
}

Expired Session Cleanup

Badger will automatically remove expired session keys.

Key Collisions

By default keys are in the form scs:session:<token>. For example:

"scs:session:ZnirGwi2FiLwXeVlP5nD77IpfJZMVr6un9oZu2qtJrg"

Because the token is highly unique, key collisions are not a concern. But if you're configuring multiple session managers, both of which use badgerstore, then you may want the keys to have a different prefix depending on which session manager wrote them. You can do this by using the NewWithPrefix() method like so:

db, err := badger.Open(badger.DefaultOptions("tmp/badger"))
if err != nil {
	log.Fatal(err)
}
defer db.Close()

sessionManagerOne = scs.New()
sessionManagerOne.Store = badgerstore.NewWithPrefix(db, "scs:session:1:")

sessionManagerTwo = scs.New()
sessionManagerTwo.Store = badgerstore.NewWithPrefix(db, "scs:session:2:")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadgerStore

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

BadgerStore represents the session store.

func New

func New(db *badger.DB) *BadgerStore

New returns a new BadgerStore instance. The db parameter should be a pointer to a badger store instance.

func NewWithPrefix

func NewWithPrefix(db *badger.DB, prefix string) *BadgerStore

NewWithPrefix returns a new BadgerStore instance. The db parameter should be a pointer to a badger store instance. The prefix parameter controls the Badger key prefix, which can be used to avoid naming clashes if necessary.

func (*BadgerStore) All

func (bs *BadgerStore) All() (map[string][]byte, error)

All returns a map containing the token and data for all active (i.e. not expired) sessions in the BadgerStore instance.

func (*BadgerStore) Commit

func (bs *BadgerStore) Commit(token string, data []byte, expiry time.Time) error

Commit adds a session token and data to the BadgerStore instance with the given expiry time. If the session token already exists then the data and expiry time are updated.

func (*BadgerStore) Delete

func (bs *BadgerStore) Delete(token string) error

Delete removes a session token and corresponding data from the BadgerStore instance.

func (*BadgerStore) Find

func (bs *BadgerStore) Find(token string) ([]byte, bool, error)

Find returns the data for a given session token from the BadgerStore instance. If the session token is not found or is expired, the returned exists flag will be set to false.

Jump to

Keyboard shortcuts

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