boltstore/README.md

87 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2014-06-17 10:05:27 +04:00
# BoltStore - Session store using Bolt
2014-06-13 13:46:18 +04:00
2014-06-17 10:02:33 +04:00
[![wercker status](https://app.wercker.com/status/752959ce0f923476671e49fb9b76ebe0/m "wercker status")](https://app.wercker.com/project/bykey/752959ce0f923476671e49fb9b76ebe0)
2016-02-18 12:08:51 +03:00
[![Coverage Status](https://coveralls.io/repos/admpub/boltstore/badge.png?branch=HEAD)](https://coveralls.io/r/admpub/boltstore)
[![GoDoc](http://godoc.org/github.com/admpub/boltstore?status.png)](http://godoc.org/github.com/admpub/boltstore)
2014-06-13 22:49:20 +04:00
2014-06-18 09:55:37 +04:00
## Overview
2014-06-13 22:50:18 +04:00
2016-02-18 12:08:51 +03:00
BoltStore is a session store using [Bolt](https://github.com/boltdb/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](https://github.com/admpub/sessions) package's [Store](http://godoc.org/github.com/admpub/sessions#Store) interface. BoltStore's APIs and examples can be seen on its [GoDoc](http://godoc.org/github.com/admpub/boltstore) page.
2014-06-13 22:50:18 +04:00
2014-06-13 22:49:20 +04:00
## Installation
```go
2016-02-18 12:08:51 +03:00
go get github.com/admpub/boltstore/...
2014-06-13 22:49:20 +04:00
```
## Example
2016-02-18 12:08:51 +03:00
Here is a simple example using BoltStore. You can see other examples on the BoltStore's [GoDoc](http://godoc.org/github.com/admpub/boltstore) page.
2014-06-18 09:55:37 +04:00
2014-06-13 22:49:20 +04:00
```go
2014-06-14 20:53:34 +04:00
package main
import (
"fmt"
"net/http"
"github.com/boltdb/bolt"
2016-02-18 12:08:51 +03:00
"github.com/admpub/sessions"
"github.com/admpub/boltstore/reaper"
"github.com/admpub/boltstore/store"
2014-06-13 22:49:20 +04:00
)
2014-06-14 20:53:34 +04:00
var db *bolt.DB
func handler(w http.ResponseWriter, r *http.Request) {
2014-06-18 06:55:28 +04:00
// Create a store.
2014-06-18 05:46:53 +04:00
str, err := store.New(db, store.Config{}, []byte("secret-key"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2014-06-14 20:53:34 +04:00
// Get a session.
session, err := str.Get(r, "session-key")
if err != nil {
2014-06-18 05:46:53 +04:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2014-06-14 20:53:34 +04:00
}
2014-06-18 05:46:53 +04:00
// Add a value on the session.
2014-06-14 20:53:34 +04:00
session.Values["foo"] = "bar"
2014-06-14 20:59:02 +04:00
// Save the session.
2014-06-14 20:53:34 +04:00
if err := sessions.Save(r, w); err != nil {
2014-06-18 05:46:53 +04:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2014-06-14 20:53:34 +04:00
}
2014-06-13 22:49:20 +04:00
2014-06-14 20:53:34 +04:00
fmt.Fprintf(w, "Hello BoltStore")
2014-06-13 22:49:20 +04:00
}
2014-06-14 20:53:34 +04:00
func main() {
2014-06-18 05:46:53 +04:00
var err error
2014-06-14 20:59:02 +04:00
// Open a Bolt database.
2014-06-22 20:43:34 +04:00
db, err = bolt.Open("./sessions.db", 0666, nil)
2014-06-18 05:46:53 +04:00
if err != nil {
panic(err)
}
2014-06-14 20:53:34 +04:00
defer db.Close()
2014-06-18 05:46:53 +04:00
// Invoke a reaper which checks and removes expired sessions periodically.
2014-06-14 20:53:34 +04:00
defer reaper.Quit(reaper.Run(db, reaper.Options{}))
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
2014-06-13 22:49:20 +04:00
}
```
2014-06-18 09:55:37 +04:00
## Benchmarks
```sh
2014-06-18 10:03:41 +04:00
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
2014-06-18 09:55:37 +04:00
BenchmarkStore_Save_delete 5000 476563 ns/op 59576 B/op 76 allocs/op
```
2014-06-13 22:49:20 +04:00
## Documentation
2016-02-18 12:08:51 +03:00
* [GoDoc](http://godoc.org/github.com/admpub/boltstore)