Go實戰--也許最快的Go語言Web框架kataras/iris初識三(Redis、leveldb、BoltDB)
生命不止,繼續 go go go !!!
繼續跟大家一起學習iris框架.
Redis
啟動Windows上redis服務
credis-server.exe redis.windows.conf
如果出現[9376] 25 Oct 15:09:11.726 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
,證明啟動成功。
main.go:
package main
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/sessions"
"github.com/kataras/iris/sessions/sessiondb/redis"
"github.com/kataras/iris/sessions/sessiondb/redis/service"
)
func main() {
db := redis.New(service.Config{
Network: "tcp",
Addr: "127.0.0.1:6379",
Password: "",
Database: "" ,
MaxIdle: 0,
MaxActive: 10,
IdleTimeout: service.DefaultRedisIdleTimeout,
Prefix: ""}) // optionally configure the bridge between your redis server
// use go routines to query the database
db.Async(true)
// close connection when control+C/cmd+C
iris.RegisterOnInterrupt(func () {
db.Close()
})
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid", Expires: 45 * time.Minute})
sess.UseDatabase(db)
// the rest of the code stays the same.
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
})
app.Get("/set", func(ctx context.Context) {
s := sess.Start(ctx)
//set session values
s.Set("name", "iris")
//test if setted here
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
})
app.Get("/get", func(ctx context.Context) {
// get a specific key, as string, if no found returns just an empty string
name := sess.Start(ctx).GetString("name")
ctx.Writef("The name on the /set was: %s", name)
})
app.Get("/delete", func(ctx context.Context) {
// delete a specific key
sess.Start(ctx).Delete("name")
})
app.Get("/clear", func(ctx context.Context) {
// removes all entries
sess.Start(ctx).Clear()
})
app.Get("/destroy", func(ctx context.Context) {
//destroy, removes the entire session data and cookie
sess.Destroy(ctx)
})
app.Get("/update", func(ctx context.Context) {
// updates expire date with a new date
sess.ShiftExpiration(ctx)
})
app.Run(iris.Addr(":8080"))
}
瀏覽器輸入:localhost:8080/set
檢視redis:
127.0.0.1:6379> keys *
1) "be019530-7f60-4a52-8e0b-81bc27cc8b9a"
127.0.0.1:6379> get be019530-7f60-4a52-8e0b-81bc27cc8b9a
"3\xff\x81\x03\x01\x01\x0bRemoteStore\x01\xff\x82\x00\x01\x02\x01\x06Values\x01\xff\x86\x00\x01\bLifetime\x01\xff\x88\x00\x00\x00\x14\xff\x85\x02\x01\x01\x05Store\x01\xff\x86\x00\x01\xff\x84\x00\x00(\xff\x83\x03\x01\x01\x05Entry\x01\xff\x84\x00\x01\x02\x01\x03Key\x01\x0c\x00\x01\bValueRaw\x01\x10\x00\x00\x00\x14\xff\x87\x05\x01\x01\bLifeTime\x01\xff\x88\x00\x00\x00\x10\xff\x89\x05\x01\x01\x04Time\x01\xff\x8a\x00\x00\x00-\xff\x82\x01\x01\x01\x04name\x01\x06string\x0c\x06\x00\x04iris\x00\x01\x0f\x01\x00\x00\x00\x0e\xd1\x82QK,\xd7\x86\x98\x01\xe0\x00"
127.0.0.1:6379>
leveldb
LevelDB是一個由Google公司所研發的鍵/值對(Key/Value Pair)嵌入式資料庫管理系統程式設計庫.
syndtr/goleveldb
這是golang中比較有名的關於leveldb的庫,我們有機會之後再學習。
main.go:
package main
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/sessions"
"github.com/kataras/iris/sessions/sessiondb/leveldb"
)
func main() {
db, _ := leveldb.New("./")
// use different go routines to sync the database
db.Async(true)
// close and unlock the database when control+C/cmd+C pressed
iris.RegisterOnInterrupt(func() {
db.Close()
})
sess := sessions.New(sessions.Config{
Cookie: "sessionscookieid",
Expires: 45 * time.Minute, // <=0 means unlimited life
})
//
// IMPORTANT:
//
sess.UseDatabase(db)
// the rest of the code stays the same.
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
})
app.Get("/set", func(ctx context.Context) {
s := sess.Start(ctx)
//set session values
s.Set("name", "iris")
//test if setted here
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
})
app.Get("/set/{key}/{value}", func(ctx context.Context) {
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
s := sess.Start(ctx)
// set session values
s.Set(key, value)
// test if setted here
ctx.Writef("All ok session setted to: %s", s.GetString(key))
})
app.Get("/get", func(ctx context.Context) {
// get a specific key, as string, if no found returns just an empty string
name := sess.Start(ctx).GetString("name")
ctx.Writef("The name on the /set was: %s", name)
})
app.Get("/get/{key}", func(ctx context.Context) {
// get a specific key, as string, if no found returns just an empty string
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
ctx.Writef("The name on the /set was: %s", name)
})
app.Get("/delete", func(ctx context.Context) {
// delete a specific key
sess.Start(ctx).Delete("name")
})
app.Get("/clear", func(ctx context.Context) {
// removes all entries
sess.Start(ctx).Clear()
})
app.Get("/destroy", func(ctx context.Context) {
//destroy, removes the entire session data and cookie
sess.Destroy(ctx)
})
app.Get("/update", func(ctx context.Context) {
// updates expire date with a new date
sess.ShiftExpiration(ctx)
})
app.Run(iris.Addr(":8080"))
}
執行後,檢視資料夾:
BoltDB
BoltDB是一個嵌入式key/value的資料庫,即只需要將其連結到你的應用程式程式碼中即可使用BoltDB提供的API來高效的存取資料。而且BoltDB支援完全可序列化的ACID事務,讓應用程式可以更簡單的處理複雜操作。
BoltDB設計源於LMDB,具有以下特點:
直接使用API存取資料,沒有查詢語句;
支援完全可序列化的ACID事務,這個特性比LevelDB強;
資料儲存在記憶體對映的檔案裡。沒有wal、執行緒壓縮和垃圾回收;
通過COW技術,可實現無鎖的讀寫併發,但是無法實現無鎖的寫寫併發,這就註定了讀效能超高,但寫效能一般,適合與讀多寫少的場景。
最後,BoltDB使用Golang開發,而且被應用於influxDB專案作為底層儲存。
LevelDB和BoltDB的不同
LevelDB是Google開發的,也是一個k/v的儲存資料庫,和BoltDB比起起來有很大的不同。對於使用者而言,最大的不同就是LevelDB沒有事務。在其內部,也有很多的不同:LevelDB實現了一個日誌結構化的merge tree。它將有序的key/value儲存在不同檔案的之中,並通過“層級”把它們分開,並且週期性地將小的檔案merge為更大的檔案。這讓其在隨機寫的時候會很快,但是讀的時候卻很慢。這也讓LevelDB的效能不可預知:但資料量很小的時候,它可能效能很好,但是當隨著資料量的增加,效能只會越來越糟糕。而且做merge的執行緒也會在伺服器上出現問題。LevelDB是C++寫的,但是也有些Go的實現方式,如syndtr/goleveldb、leveldb-go。
BoltDB使用一個單獨的記憶體對映的檔案,實現一個寫入時拷貝的B+樹,這能讓讀取更快。而且,BoltDB的載入時間很快,特別是在從crash恢復的時候,因為它不需要去通過讀log(其實它壓根也沒有)去找到上次成功的事務,它僅僅從兩個B+樹的根節點讀取ID。
Star: 7144
獲取:
go get github.com/boltdb/bolt/…
iris中使用boltDB:
package main
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/sessions"
"github.com/kataras/iris/sessions/sessiondb/boltdb"
)
func main() {
db, _ := boltdb.New("./sessions.db", 0666, "users")
// use different go routines to sync the database
db.Async(true)
// close and unlock the database when control+C/cmd+C pressed
iris.RegisterOnInterrupt(func() {
db.Close()
})
sess := sessions.New(sessions.Config{
Cookie: "sessionscookieid",
Expires: 45 * time.Minute, // <=0 means unlimited life
})
//
// IMPORTANT:
//
sess.UseDatabase(db)
// the rest of the code stays the same.
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
})
app.Get("/set", func(ctx context.Context) {
s := sess.Start(ctx)
//set session values
s.Set("name", "iris")
//test if setted here
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
})
app.Get("/set/{key}/{value}", func(ctx context.Context) {
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
s := sess.Start(ctx)
// set session values
s.Set(key, value)
// test if setted here
ctx.Writef("All ok session setted to: %s", s.GetString(key))
})
app.Get("/get", func(ctx context.Context) {
// get a specific key, as string, if no found returns just an empty string
name := sess.Start(ctx).GetString("name")
ctx.Writef("The name on the /set was: %s", name)
})
app.Get("/get/{key}", func(ctx context.Context) {
// get a specific key, as string, if no found returns just an empty string
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
ctx.Writef("The name on the /set was: %s", name)
})
app.Get("/delete", func(ctx context.Context) {
// delete a specific key
sess.Start(ctx).Delete("name")
})
app.Get("/clear", func(ctx context.Context) {
// removes all entries
sess.Start(ctx).Clear()
})
app.Get("/destroy", func(ctx context.Context) {
//destroy, removes the entire session data and cookie
sess.Destroy(ctx)
})
app.Get("/update", func(ctx context.Context) {
// updates expire date with a new date
sess.ShiftExpiration(ctx)
})
app.Run(iris.Addr(":8080"))
}