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

标签 go redis go-gin

我在 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/

相关文章:

linux - 如何使用 Golang 安装 Gin

redis - 使用 SignalR 和 Redis 的组

scala - Redis 与 Redisson 框架在 Scala 中的不可预测行为

go - 来自 `gin-gonic` 文档的语法问题

go - 使用互斥锁和反射时的竞争条件

mysql - 通过 Redis 使用 GET 负载均衡 KEY

golang gin 加载 html 超时

google-app-engine - 无法将 []datastore.PropertyList 传递给 GetMulti 函数(数据存储 : src has invalid type)

go - 我在运行以下代码时遇到索引超出范围错误

string - 无法检查使用 reader.ReadString 捕获的字符串上的 strings.HasSuffix