英文:
Configure gin-gonic session using Redis in Golang
问题
我正在使用Go语言中的gin-gonic框架,并使用github.com/gin-gonic/contrib/sessions
包中提供的Redis会话功能。
store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
router.Use(sessions.Sessions("workino_session", store))
我该如何控制这些会话在Redis中存储的时间?
谢谢。
英文:
I am using gin-gonic in Go and using Redis session feature provided in github.com/gin-gonic/contrib/sessions
package
store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
router.Use(sessions.Sessions("workino_session", store))
How do I control how long these Sessions are stored in Redis?
Thank you.
答案1
得分: 2
尽管 README 文档中的说明很简略,但是 GoDoc 文档 对此有更清晰的解释。
请注意,gin-gonic sessions 包使用 gorilla/sessions 库作为底层,并共享相同的选项 API。
// 我们检查错误。
store, err := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
if err != nil {
// 处理错误。如果无法连接,可能需要退出。
}
// 参考:https://godoc.org/github.com/gin-gonic/contrib/sessions#Options
store.Options = &sessions.Options{
MaxAge: 86400,
Path: "/",
Secure: true,
HttpOnly: true,
}
// 配置完成后使用存储。
router.Use(sessions.Sessions("workino_session", store))
英文:
Although the README is light on documentation, the GoDoc docs are a little more clear about this.
Note that the gin-gonic sessions package uses gorilla/sessions underneath and shares the same options API.
// We check for errors.
store, err := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
if err != nil {
// Handle the error. Probably bail out if we can't connect.
}
// Ref: https://godoc.org/github.com/gin-gonic/contrib/sessions#Options
store.Options = &sessions.Options{
MaxAge: 86400,
Path: "/",
Secure: true,
HttpOnly: true,
}
// Use the store once configured.
router.Use(sessions.Sessions("workino_session", store))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论