You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 6 Next »
内存存储比较简单,性能也很高效,但没有持久化存储Session数据,因此应用程序重启之后便会丢失Session数据,可用于特定的业务场景中。gsession的内存存储使用StorageMemory对象实现,
Session
gsession
内存
StorageMemory
https://github.com/gogf/gf/v2/blob/master/.example/os/gsession/storage-memory/memory.go
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分钟。执行后,
1分钟