阅读(3966) (0)

GoFrame Session-Memory

2022-04-12 11:25:22 更新

内存存储

内存存储比较简单,性能也很高效,但没有持久化存储​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.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. 首先,访问 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​变量已经没有了;