0

I am working with fiber golang framework. I can't figure out why I cannot get the value set in the store(Redis in this case) from another request or within. Below is the code:

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

I've tried to get it from within the same request, it seems to work just before calling store.Save() but not working after! it just returns nil

  • it seems to work just before calling store.Save() but not working after! it just returns nil. Can you elaborate this line ! – Saurav Kumar Singh Aug 11 '20 at 07:53
  • ie. Before calling `store.Save` in the same request I can get the value by calling `store.Get("name')` For instance in the case where I defer `store.Save()`. But after calling `store.Save()` is gonna return `` For instance in the case where I don't defer and I save before trying to access the value(save before get) . But accessing it from a different request is not going to work at all:( – Dilunga the Great Aug 11 '20 at 16:30

1 Answers1

0

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
    
})
KAMAL
  • 41
  • 1
  • 1
    Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit](https://stackoverflow.com/posts/65609986/edit) your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Jan 07 '21 at 10:29