无法从Gofiber会话中的会话存储中获取价值

问题描述

我正在使用光纤golang框架。我不明白为什么我不能从另一个请求或内部请求中获得存储(在此情况下为Redis)中设置的值。下面是代码

sessionProvider := redis.New(redis.Config{
    KeyPrefix:   "session",Addr:        "127.0.0.1:6379",PoolSize:    8,IdleTimeout: 30 * time.Second,})
sessions := session.New(session.Config{
    Provider: sessionProvider,})

// sample routes for testing session
app.Get("/testing1",func(c *fiber.Ctx) {
    store := sessions.Get(c)
    //    set value to the session store
    store.Set("name","King Windrol")
    store.Save()
})
app.Get("/testing2",func(c *fiber.Ctx) {
    store := sessions.Get(c)
    c.Send(store.Get("name"))
})

我尝试从同一请求中获取它,它似乎在调用store.Save()之前就可以使用,但之后却无法使用!它只会返回nil

解决方法

app.Get("/testing1",func(c *fiber.Ctx) {
    store := sessions.Get(c)
    //    set value to the session store
    store.Set("name","King Windrol")
    store.Save()
    c.SendStatus(200) //add this
    
})