Go实战--也许最快的Go语言Web框架kataras/iris初识三(Redis、leveldb、BoltDB)
生命不止,繼續 go go go !!!
之前介紹了iris框架,介紹了如何使用basic認證、Markdown、YAML、Json等:?
Go實戰–也許最快的Go語言Web框架kataras/iris初識(basic認證、Markdown、YAML、Json)
介紹了如何使用TOML、Cache、Cookie等:?
Go實戰–也許最快的Go語言Web框架kataras/iris初識二(TOML、Cache、Cookie)
繼續跟大家一起學習iris框架.
Redis
關于redis,之前也有介紹過:?
Go實戰–golang中使用redis(redigo和go-redis/redis)
Go實戰–通過httprouter和redis框架搭建restful api服務(github.com/julienschmidt/httprouter)
啟動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 mainimport ("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 databasedb.Async(true)// close connection when control+C/cmd+Ciris.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 valuess.Set("name", "iris")//test if setted herectx.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 stringname := 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 keysess.Start(ctx).Delete("name")})app.Get("/clear", func(ctx context.Context) {// removes all entriessess.Start(ctx).Clear()})app.Get("/destroy", func(ctx context.Context) {//destroy, removes the entire session data and cookiesess.Destroy(ctx)})app.Get("/update", func(ctx context.Context) {// updates expire date with a new datesess.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 mainimport ("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 databasedb.Async(true)// close and unlock the database when control+C/cmd+C pressediris.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 valuess.Set("name", "iris")//test if setted herectx.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 valuess.Set(key, value)// test if setted herectx.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 stringname := 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 stringname := 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 keysess.Start(ctx).Delete("name")})app.Get("/clear", func(ctx context.Context) {// removes all entriessess.Start(ctx).Clear()})app.Get("/destroy", func(ctx context.Context) {//destroy, removes the entire session data and cookiesess.Destroy(ctx)})app.Get("/update", func(ctx context.Context) {// updates expire date with a new datesess.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。
github地址:?
https://github.com/boltdb/bolt
Star: 7144
獲取:?
go get github.com/boltdb/bolt/…
iris中使用boltDB:
package mainimport ("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 databasedb.Async(true)// close and unlock the database when control+C/cmd+C pressediris.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 valuess.Set("name", "iris")//test if setted herectx.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 valuess.Set(key, value)// test if setted herectx.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 stringname := 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 stringname := 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 keysess.Start(ctx).Delete("name")})app.Get("/clear", func(ctx context.Context) {// removes all entriessess.Start(ctx).Clear()})app.Get("/destroy", func(ctx context.Context) {//destroy, removes the entire session data and cookiesess.Destroy(ctx)})app.Get("/update", func(ctx context.Context) {// updates expire date with a new datesess.ShiftExpiration(ctx)})app.Run(iris.Addr(":8080")) }總結
以上是生活随笔為你收集整理的Go实战--也许最快的Go语言Web框架kataras/iris初识三(Redis、leveldb、BoltDB)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go实战--也许最快的Go语言Web框架
- 下一篇: 使用Docker中的mysql