zdpgo_gin_sessions 专为zdpgo_gin打造的session框架,当需要使用session存储数据的时候可以考虑使用此框架

zdpgo_gin_sessions

专为zdpgo_gin打造的session框架,当需要使用session存储数据的时候可以考虑使用此框架

安装

bash 复制代码

使用教程

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
相关推荐
吴佳浩2 天前
Gin 入门指南 Swagger aipfox集成
后端·go·gin
梦想画家2 天前
Golang Gin系列-6:Gin 高级路由及URL参数
golang·gin
m0_748244832 天前
Go-Gin Web 框架完整教程
前端·golang·gin
米饭好好吃.4 天前
【Go】Go Gin框架初识(一)
golang·gin
梦想画家4 天前
Golang Gin系列-4:Gin Framework入门教程
golang·gin
Ri0n4 天前
Gin 源码概览 - 路由
gin
梦想画家5 天前
Golang Gin系列-3:Gin Framework的项目结构
golang·gin
梦想画家5 天前
Golang Gin系列-2:搭建Gin 框架环境
golang·gin
梦想画家6 天前
Golang Gin系列-1:Gin 框架系列教程
golang·gin
{⌐■_■}7 天前
【gin】中间件使用之jwt身份认证和Cors跨域,go案例
中间件·golang·gin