Session store using Bolt
Find a file
2023-10-15 11:58:04 +08:00
.vscode update 2017-07-22 12:18:03 +08:00
reaper fix -- for echo framework 2016-02-18 17:08:51 +08:00
shared update 2017-07-22 12:18:03 +08:00
store update 2023-10-15 11:58:04 +08:00
.gitignore update 2021-05-19 12:00:05 +08:00
LICENSE Initial commit 2014-06-13 18:46:18 +09:00
Makefile Update Makefile 2014-06-18 15:04:12 +09:00
README.md fix -- for echo framework 2016-02-18 17:08:51 +08:00
wercker.yml Update wercker.yml 2014-10-01 14:16:55 +09:00

BoltStore - Session store using Bolt

wercker status Coverage Status GoDoc

Overview

BoltStore is a session store using Bolt which is a pure Go key/value store. You can store session data in Bolt by using this store. This store implements the gorilla/sessions package's Store interface. BoltStore's APIs and examples can be seen on its GoDoc page.

Installation

go get github.com/admpub/boltstore/...

Example

Here is a simple example using BoltStore. You can see other examples on the BoltStore's GoDoc page.

package main

import (
	"fmt"
	"net/http"

	"github.com/boltdb/bolt"
	"github.com/admpub/sessions"
	"github.com/admpub/boltstore/reaper"
	"github.com/admpub/boltstore/store"
)

var db *bolt.DB

func handler(w http.ResponseWriter, r *http.Request) {
	// Create a store.
	str, err := store.New(db, store.Config{}, []byte("secret-key"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	// Get a session.
	session, err := str.Get(r, "session-key")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

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

	// Save the session.
	if err := sessions.Save(r, w); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	fmt.Fprintf(w, "Hello BoltStore")
}

func main() {
	var err error
	// Open a Bolt database.
	db, err = bolt.Open("./sessions.db", 0666, nil)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	// Invoke a reaper which checks and removes expired sessions periodically.
	defer reaper.Quit(reaper.Run(db, reaper.Options{}))
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

Benchmarks

BenchmarkNew	    			5000	    316700 ns/op	   19003 B/op	      35 allocs/op
BenchmarkStore_Get			20000000	       104 ns/op	       0 B/op	       0 allocs/op
BenchmarkStore_New			10000000	       294 ns/op	     130 B/op	       2 allocs/op
BenchmarkStore_Save	    		5000	    488683 ns/op	   65484 B/op	     136 allocs/op
BenchmarkStore_Save_delete	    5000	    476563 ns/op	   59576 B/op	      76 allocs/op

Documentation