生命不止,继续 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)相关推荐

  1. Go实战--也许最快的Go语言Web框架kataras/iris初识二(TOML、Cache、Cookie)

    生命不止,继续 go go go!!! 昨天介绍了iris框架,介绍了如何使用basic认证.Markdown.YAML.Json等:  Go实战–也许最快的Go语言Web框架kataras/iris ...

  2. Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)

    生命不止,继续 go go go !!! 接下来,想跟大家一起分享一些golang语言成熟的.知名度比较高的web框架. 我们从iris web框架开始,开始呢,我们先不去计较和比较谁的速度快,谁的性 ...

  3. Go实战--也许最快的Go语言Web框架kataras/iris初识四(i18n、filelogger、recaptcha)

    生命不止,继续 go go go !!! 继续分享关于kataras/iris框架 i18n i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数) ...

  4. go java web框架_java程序员10分钟可上手的golang框架golang实战使用gin+xorm搭建go语言web框架restgo...

    1.首先上效果 是不是想起spring MVC? cd $GOPATH/src git clone https://github.com/winlion/restgo-admin.git 你将得到re ...

  5. 干货分享:六个知名的Go语言web框架

    框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写程序了.成长总不会一蹴而就,从写出程序获取成就感,再到精通框架,快速构造应用,当这些方面都得心应 ...

  6. go web框架_干货分享:六个知名的Go语言web框架

    框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写程序了.成长总不会一蹴而就,从写出程序获取成就感,再到精通框架,快速构造应用,当这些方面都得心应 ...

  7. Go语言web框架 gin

    Go语言web框架 GIN gin是go语言环境下的一个web框架, 它类似于Martini, 官方声称它比Martini有更好的性能, 比Martini快40倍, Ohhhh-.看着不错的样子, 所 ...

  8. 流行的Go语言web框架简介

    Golang被称为云计算时代的C语言,它以其独特的优势逐渐被越来越多的公司所关注和使用.为了充分利用Golang的Web开发优势,有必要熟悉一下Go语言的web框架. 1  Beego (http:/ ...

  9. spring框架 web开发_go语言web开发框架:Iris框架讲解(一)

    Golang介绍 Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性.谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发Go,是因为过去10多年间软 ...

最新文章

  1. redis 未授权访问详解
  2. Android UI(继承控件)--PopupWindow设置动画
  3. 率土之滨鸿蒙之初,率土之滨:最记仇联盟?投诚玩家结算前被乱世,称是主盟要求...
  4. gradle wrapper 版本与 android build tool 版本匹配要求
  5. mysql 6.17_2020 6/17 mysql数据的增删改查
  6. UE4 Roadmap
  7. ubuntu18.04升级cmake
  8. php下载安装教程,PHP下载安装教程
  9. VTD的官方help翻译-ROD部分(1~4章)
  10. IIS5 IIS6 IIS7区别
  11. 基于Matlab的自适应低通滤波器设计,基于MATLAB的自适应滤波器的设计
  12. win10如何强制删除文件
  13. Bounds用法参考
  14. LG30刷小米系统_小米红米手机安卓9底层如何正确安全升级安卓10版MIUI12开发版...
  15. 提示格式化怎么修复??
  16. Springer出版社旗下投稿word模板
  17. 计算机软考什么时候出分,2020年计算机软考什么时候出成绩,怎么查成绩?|...
  18. 实现resolv.conf永久设置的方法
  19. grant with admin option and grant with grant option
  20. Vim快速移动光标至行首和行尾 、第一行和最后一行

热门文章

  1. 注意啦!10 个你需要了解的 Linux 网络和监控命令
  2. BZOJ 1001: [BeiJing2006]狼抓兔子【最大流/SPFA+最小割,多解】
  3. Hello,Expression Blend 4 (含Demo教程和源码)
  4. 基于量子粒子群算法实现天线阵列优化
  5. python 图形界面库对比
  6. 动态链接库dll的两种加载方式
  7. Linux系统中sysctl命令详解 sysctl -p、sysctl -a、sysctl -w
  8. linux下scp远程拷贝文件无需输入密码工具之expect
  9. WinSock五种I/O模型的性能分析
  10. 智能老旧模糊照片修复——C++实现GFPGAN模型推理