cedar-sessioncedar-router 的 session 组件

联合创作 · 2023-10-01 11:23

cedar-router 的一个 session 组件。

2 个方法

  • Set(string,interface{})
  • Get(string) interface{}

使用方法

r:=cedar.NewRouter()

x := cedar_session.NewSession(r)

x 继承所有的 http method 和 group ,只是有些小改动 ,不能直接使用 ,只能在 http method 中使用

从之前的 

r.Get("/",func(w http.ResponseWriter, r *http.Request,nil)
r.Group("/a", func(groups *cedar.Groups) {}

到现在的

x.Get("/", func(w http.ResponseWriter, r *http.Request, s cedar_session.Session), nil)
x.Group("/a", func(groups *cedar_session.TheGroup) {}

改了参数 ,更多 例子 ,现在使用的是系统自带的map ,可能以后会换成 spruce ,分布式的kv数据库

例子

r := cedar.NewRouter()
x := cedar_session.NewSession(r)
x.Get("/set", func(w http.ResponseWriter, r *http.Request, s cedar_session.Session) {
	s.Set("hello", "world"+r.RemoteAddr) // 设置session
    s.Get("hello")                       // 获取session
	w.Write([]byte("hello session"))
}, nil)

 

浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报