Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

内存存储

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

使用示例

Code Block
languagego
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":  SetSessionMaxAge(time.Minute,)
		"SessionStorage": s.SetSessionStorage(gsession.NewStorageMemory(),
	})
	s.Group("/", func(group *ghttp.RouterGroup) {
		group.ALL("/set", func(r *ghttp.Request) {
			r.Session.SetMustSet("time", gtime.Timestamp())
			r.Response.Write("ok")
		})
		group.ALL("/get", func(r *ghttp.Request) {
			r.Response.Write(r.Session.MapData())
		})
		group.ALL("/del", func(r *ghttp.Request) {
			_ = r.Session.ClearRemoveAll()
			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变量已经没有了;



Panel
titleContent Menu

Table of Contents