Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
内存存储
内存存储比较简单,性能也很高效,但没有持久化存储Session
数据,因此应用程序重启之后便会丢失Session
数据,可用于特定的业务场景中。gsession
的内存
存储使用StorageMemory
对象实现,
使用示例
https://github.com/gogf/gf/v2/blob/master/.example/os/gsession/storage-memory/memory.go
Code Block | ||
---|---|---|
| ||
package main import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gsession" "github.com/gogf/gf/v2/os/gtime" "time" ) func main() { s := g.Server() s.SetConfigWithMap(g.Map{ "SessionMaxAge": time.Minute, "SessionStorage": gsession.NewStorageMemory(), }) s.Group("/", func(group *ghttp.RouterGroup) { group.ALL("/set", func(r *ghttp.Request) { r.Session.Set("time", gtime.Timestamp()) r.Response.Write("ok") }) group.ALL("/get", func(r *ghttp.Request) { r.Response.Write(r.Session.Map()) }) group.ALL("/del", func(r *ghttp.Request) { r.Session.Clear() r.Response.Write("ok") }) }) s.SetPort(8199) s.Run() } |
在该实例中,为了方便观察过期失效,我们将Session
的过期时间设置为1分钟
。执行后,
- 首先,访问 http://127.0.0.1:8199/set 设置一个
Session
变量; - 随后,访问 http://127.0.0.1:8199/get 可以看到该
Session
变量已经设置并成功获取; - 接着,我们停止程序,并重新启动,再次访问 http://127.0.0.1:8199/get ,可以看到
Session
变量已经没有了;
Panel | ||
---|---|---|
| ||
|