gpt4 book ai didi

go - 在 Golang 中使用 Redis 配置 gin-gonic session

转载 作者:可可西里 更新时间:2023-11-01 11:20:39 29 4
gpt4 key购买 nike

我在 Go 中使用 gin-gonic 并使用 github.com/gin-gonic/contrib/sessions 包中提供的 Redis session 功能

store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
router.Use(sessions.Sessions("workino_session", store))

我如何控制这些 Session 在 Redis 中存储多长时间?

谢谢。

最佳答案

尽管 README 文档很少,GoDoc docs对此更清楚一些。

请注意,gin-gonic session 包使用 gorilla/sessions在下面并共享相同的选项 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))

关于go - 在 Golang 中使用 Redis 配置 gin-gonic session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35473916/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com