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框架
gin
TMDOG6662 天前
TMDOG的Gin学习笔记_02——Gin集成支付宝支付沙箱环境
笔记·学习·gin
5pace2 天前
GIN:逼近WL-test的GNN架构
gin
RationalDysaniaer2 天前
gin入门
笔记·gin
清北_2 天前
Go常见框架对比
前端·golang·gin
还是转转3 天前
Go开发指南-Gin与Web开发
golang·gin
RationalDysaniaer7 天前
Gin入门笔记
笔记·gin
千年死缓7 天前
gin中间件
中间件·gin
codists12 天前
《使用Gin框架构建分布式应用》阅读笔记:p393-p437
golang·gin·编程人
景天科技苑12 天前
【Golang】Gin框架中如何使用JWT来实现登录认证
开发语言·golang·gin·jwt·登录认证·go jwt