gorilla/sessions在服务器重新启动后是否持久化?

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

gorilla/sessions persistent between server restarts?

问题

我对会话有一个一般性的问题。对于这个主题,我并不是很熟悉。我尝试过以下方法:

  • NewRediStore (gopkg.in/boj/redistore.v1)
  • NewCookieStore
  • NewFileSystemStore

我原本以为会话可以在服务器重新启动之间保持,因此需要一个“存储”机制。当我的 Golang 后端正在运行时,我可以为多个用户/浏览器设置新的会话并检索它们,没有任何问题。

但是,当我重新启动服务器后,我注意到所有的会话访问结果都是 session.IsNew == true。在 Redis 中,我可以看到重新启动后的所有会话键,并且甚至验证了 .Get 方法返回了正确的 ID,但 IsNew 仍然被设置为 true。

我猜直觉上讲这是有道理的,因为可能存在某个内存中的映射导致了 IsNew 的设置,但我认为如果存储中存在与 cookie 键匹配的数据,IsNew 不应该被设置为 true。我是不是疯了?我是否做错了什么简单的事情?这是对如何使用会话的基本误解吗?如果需要,我可以提供代码或其他详细信息,请告诉我。

英文:

I have a general question about sessions. I am not very seasoned when it comes to this subject. I've tried with:

NewRediStore (gopkg.in/boj/redistore.v1)  
NewCookieStore  
NewFileSystemStore

I was under the impression that sessions could last between server restarts, hence the need for a 'store'. While my golang backend is running, I am able to set new sessions and retrieve them for multiple users/browsers. No problems there.

When I restart my server, I notice that all session access results in session.IsNew == true.
In Redis, I can see all the session keys after the restart, and even verified that .Getting the session results in the right ID retrieved, but IsNew is still set.

I guess intuitively, this makes sense because there must be some map in memory that leads to the setting of IsNew but I would think that if there was any hit for the cookie key in the store, IsNew should not be set. Am I going crazy? Is there something easy that I am doing wrong? Is this a fundamental misunderstanding of how to use sessions?
Please let me know if I need to include code or additional details.

答案1

得分: 1

我会翻译以下内容:

我会有和你一样的假设,并且浏览源代码后,看起来它应该按照你描述的方式工作。你可以尝试进行调试并逐步执行它,特别是你正在使用的存储的 New 方法(例如 FilesystemStore.NewRediStore.New)。如果该方法成功读取 cookie 并在存储中找到会话,它应该根据源代码设置 IsNew = false

此外,请注意,仅仅检查会话 ID 不是验证这种行为的好方法。如果你查看源代码,它会从 cookie 中解码会话 ID,然后尝试在后端存储中查找。如果查找失败,那么会话 ID 将匹配,但 IsNew 将为 true,并且会话中不会有任何值。确保在会话中设置某些值,并检查该值而不是会话 ID。对于 CookieStore,行为不同,因为它将会话数据存储在 cookie 本身中。

英文:

I would have had the same assumptions you did, and browsing the source, it looks like it should work as you described. You might try debugging and stepping through it, particularly the New method for the store you're using (e.g. FilesystemStore.New or RediStore.New). If that method successfully reads the cookie and finds the session in the store, it should set IsNew = false, according to the source.

Also note that just checking the session ID is not a good way of validating this behavior. If you look at the source, it decodes the session ID from the cookie, then tries to look that up in the backing store. If the lookup fails, then the session ID will match, but IsNew will be true and there won't be any values in the session. Make sure you're setting some value in the session and check for that instead of the session ID. The behavior is different for the CookieStore since it stores the session data in the cookie itself.

huangapple
  • 本文由 发表于 2017年7月20日 01:09:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/45196950.html
匿名

发表评论

匿名网友

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

确定