github.com/gogf/gf@v1.16.9/.example/net/ghttp/server/session/redis/redis.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/gogf/gf/frame/g"
     5  	"github.com/gogf/gf/net/ghttp"
     6  	"github.com/gogf/gf/os/gsession"
     7  	"github.com/gogf/gf/os/gtime"
     8  	"time"
     9  )
    10  
    11  func main() {
    12  	s := g.Server()
    13  	s.SetSessionMaxAge(2 * time.Minute)
    14  	s.SetSessionStorage(gsession.NewStorageRedis(g.Redis()))
    15  	s.Group("/", func(group *ghttp.RouterGroup) {
    16  		group.GET("/set", func(r *ghttp.Request) {
    17  			r.Session.Set("time", gtime.Timestamp())
    18  			r.Response.Write("ok")
    19  		})
    20  		group.GET("/get", func(r *ghttp.Request) {
    21  			r.Response.WriteJson(r.Session.Map())
    22  		})
    23  		group.GET("/clear", func(r *ghttp.Request) {
    24  			r.Session.Clear()
    25  		})
    26  	})
    27  	s.SetPort(8199)
    28  	s.Run()
    29  }