buntstore

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2017 License: MIT Imports: 2 Imported by: 0

README

buntstore

godoc

Package buntstore is a buntdb storage engine for the SCS session package.

This is a good option for local development, or a single-server deployment. Unlike memstore the data will survive server restarts.

It provides better performance than boltdb, but only syncs to disk every second. You could lose data. You have to decide if it is acceptable for your application to loose the last second of session data.

Usage

Installation

Either:

$ go get github.com/alexedwards/scs/engine/buntstore

Or (recommended) use use some kind of vendoring

Setup
package main

import (
	"io"
	"log"
	"net/http"
	"time"

	"github.com/alexedwards/scs/engine/buntstore"
	"github.com/alexedwards/scs/session"
	"github.com/tidwall/buntdb"
)

func main() {
	// You provide a bunt.DB instance
	db, err := buntdb.Open("/tmp/bunt.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Create a new buntstore instance with bunt.DB
	engine := buntstore.New(db)

	sessionManager := session.Manage(engine)
	http.HandleFunc("/put", putHandler)
	http.HandleFunc("/get", getHandler)
	http.ListenAndServe(":4000", sessionManager(http.DefaultServeMux))
}

func putHandler(w http.ResponseWriter, r *http.Request) {
	err := session.PutString(r, "message", "Hello world!")
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
}

func getHandler(w http.ResponseWriter, r *http.Request) {
	msg, err := session.GetString(r, "message")
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
	io.WriteString(w, msg)
}

Cleaning up expired session data

The buntdb package provides a background 'cleanup' goroutine to delete expired session data. This stops the database file from holding on to invalid sessions indefinitely and growing unnecessarily large.

Additionally on every 'Get' the item is checked for expiration.

buntdb uses an AOF to store the data. It provied an autoshrink feature wich does not block the main go routine and reduces the file to current db contents.

Notes

The buntstore package is underpinned by the excellent buntdb.

Full godoc documentation: https://godoc.org/github.com/alexedwards/scs/engine/buntstore.

Documentation

Overview

Package buntstore is a buntdb based storage engine for the SCS session package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuntStore

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

BuntStore is a SCS storage engine backed by a buntdb file.

func New

func New(db *buntdb.DB) *BuntStore

New creates a BuntStore instance.

func (*BuntStore) Delete

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

Delete removes session token and corresponding data.

func (*BuntStore) Find

func (bs *BuntStore) Find(token string) (b []byte, exists bool, err error)

Find returns the data for a session token. If the session token is not found or is expired, the exists flag will be false.

func (*BuntStore) Save

func (bs *BuntStore) Save(token string, b []byte, expiry time.Time) error

Save updates data for a given session token with a given expiry. Any existing data + expiry will be over-written.

Jump to

Keyboard shortcuts

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