配置Golang中使用Redis的gin-gonic会话。

huangapple go评论81阅读模式
英文:

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))

huangapple
  • 本文由 发表于 2016年2月18日 13:57:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/35473916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定