golang session实现库 支持cookie, redis,mysql等多种存储方式

golang中官方是不支持session的, 如果想要实现session则需要自己动手来实现,或者使用第三方的go-session实现库, 今天就给大家介绍一个go语言的第三方session实现库 go-sessions,支持 的存储方式有 cookie, file, redis, mysql等众多的存储。

使用示例:

Go 复制代码
import (
		"net/http"
		"github.com/gorilla/sessions"
	)

	// Note: Don't store your key in your source code. Pass it via an
	// environmental variable, or flag (or both), and don't accidentally commit it
	// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
	// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
	var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))

	func MyHandler(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		err := session.Save(r, w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

Github项目地址:

GitHub - tekintian/go-sessions: go语言里面的Sessions实现库, 支持众多的Session存储方式,cookie, redis, mysql等。 Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.go语言里面的Sessions实现库, 支持众多的Session存储方式,cookie, redis, mysql等。 Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends. - tekintian/go-sessionshttps://github.com/tekintian/go-sessions

相关推荐
薛不痒38 分钟前
使用python操作MySQL
数据库·mysql
xiaoshujiaa41 分钟前
Java大厂面试实录:谢飞机硬刚互联网医疗微服务架构,Spring Cloud+Redis+Kafka全踩坑
spring boot·redis·微服务·kafka·flyway·java面试·互联网医疗
linksinke1 小时前
在windows系统上搭建Golang多版本管理器(g)的配置环境
开发语言·windows·golang
回忆是昨天里的海1 小时前
Spring boot接入视图时的问题
mysql·mybatisplus·视图
液态不合群1 小时前
【面试题】MySQL 中的索引数量是否越多越好?为什么?
android·数据库·mysql
3824278271 小时前
python:mysql数据库
数据库·python·mysql
半壶清水1 小时前
ubuntu中mysql升级指南
mysql·ubuntu·adb
我爱娃哈哈1 小时前
告别Redis瓶颈:Caffeine本地缓存优化实战指南
数据库·redis·缓存
G_H_S_3_1 小时前
【网络运维】MySQL组成与常用工具
运维·网络·mysql
机灵猫1 小时前
Redis 内部机制:持久化、内存淘汰与延迟优化
数据库·redis·缓存