zdpgo_gin_sessions
专为zdpgo_gin打造的session框架,当需要使用session存储数据的时候可以考虑使用此框架
安装
bash
使用教程
cookie
go
package main
import (
gin "github.com/zhangdapeng520/zdpgo_gin"
sessions "github.com/zhangdapeng520/zdpgo_gin_sessions"
"github.com/zhangdapeng520/zdpgo_gin_sessions/cookie"
)
func main() {
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
memory store
go
package main
import (
gin "github.com/zhangdapeng520/zdpgo_gin"
sessions "github.com/zhangdapeng520/zdpgo_gin_sessions"
"github.com/zhangdapeng520/zdpgo_gin_sessions/memstore"
)
func main() {
r := gin.Default()
store := memstore.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
memory cache ascii
go
package main
import (
gin "github.com/zhangdapeng520/zdpgo_gin"
sessions "github.com/zhangdapeng520/zdpgo_gin_sessions"
"github.com/zhangdapeng520/zdpgo_gin_sessions/memcached"
"github.com/zhangdapeng520/zdpgo_gin_sessions/memcached/memcache"
)
func main() {
r := gin.Default()
store := memcached.NewStore(memcache.New("localhost:11211"), "", []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
memory cache binary
go
package main
import (
gin "github.com/zhangdapeng520/zdpgo_gin"
sessions "github.com/zhangdapeng520/zdpgo_gin_sessions"
"github.com/zhangdapeng520/zdpgo_gin_sessions/memcached"
"github.com/zhangdapeng520/zdpgo_gin_sessions/memcached/mc"
)
func main() {
r := gin.Default()
client := mc.NewMC("localhost:11211", "username", "password")
store := memcached.NewMemcacheStore(client, "", []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
redis
go
package main
import (
gin "github.com/zhangdapeng520/zdpgo_gin"
sessions "github.com/zhangdapeng520/zdpgo_gin_sessions"
"github.com/zhangdapeng520/zdpgo_gin_sessions/redis"
)
func main() {
r := gin.Default()
store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
gorm mysql
go
package main
import (
gin "github.com/zhangdapeng520/zdpgo_gin"
gorm "github.com/zhangdapeng520/zdpgo_gorm"
"github.com/zhangdapeng520/zdpgo_gorm/mysql"
sessions "github.com/zhangdapeng520/zdpgo_gin_sessions"
gormsessions "github.com/zhangdapeng520/zdpgo_gin_sessions/gorm"
)
func main() {
db, err := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
if err != nil {
panic(err)
}
store := gormsessions.NewStore(db, true, []byte("secret"))
r := gin.Default()
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
版本历史
v0.1.0
- 支持常用的session
v0.1.1
- 包名从zdpgo_sessions改为zdpgo_gin_sessions