Session-Memory

内存存储

内存存储比较简单,性能也很高效,但没有持久化存储Session数据,因此应用程序重启之后便会丢失Session数据,可用于特定的业务场景中。gsession的内存存储使用StorageMemory对象实现,

使用示例

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.SetSessionMaxAge(time.Minute)
    s.SetSessionStorage(gsession.NewStorageMemory())
    s.Group("/", func(group *ghttp.RouterGroup) {
        group.ALL("/set", func(r *ghttp.Request) {
            r.Session.MustSet("time", gtime.Timestamp())
            r.Response.Write("ok")
        })
        group.ALL("/get", func(r *ghttp.Request) {
            r.Response.Write(r.Session.Data())
        })
        group.ALL("/del", func(r *ghttp.Request) {
            _ = r.Session.RemoveAll()
            r.Response.Write("ok")
        })
    })
    s.SetPort(8199)
    s.Run()
}

在该实例中,为了方便观察过期失效,我们将Session的过期时间设置为1分钟。执行后,

  1. 首先,访问 http://127.0.0.1:8199/set 设置一个Session变量;
  2. 随后,访问 http://127.0.0.1:8199/get 可以看到该Session变量已经设置并成功获取;
  3. 接着,我们停止程序,并重新启动,再次访问 http://127.0.0.1:8199/get ,可以看到Session变量已经没有了;