mirror of
https://gitee.com/johng/gf
synced 2026-07-01 11:29:48 +08:00
gsession开发
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package ghttp
|
||||
|
||||
import "gitee.com/johng/gf/g/net/gsession"
|
||||
|
||||
// 控制器基类
|
||||
type Controller struct {
|
||||
|
||||
@ -13,4 +15,25 @@ func (c *Controller) Head(*ClientRequest, *ServerResponse) {}
|
||||
func (c *Controller) Patch(*ClientRequest, *ServerResponse) {}
|
||||
func (c *Controller) Connect(*ClientRequest, *ServerResponse) {}
|
||||
func (c *Controller) Options(*ClientRequest, *ServerResponse) {}
|
||||
func (c *Controller) Trace(*ClientRequest, *ServerResponse) {}
|
||||
func (c *Controller) Trace(*ClientRequest, *ServerResponse) {}
|
||||
|
||||
// 获取当前请求的session对象
|
||||
func (c *Controller) Session(r *ClientRequest, w *ServerResponse) *gsession.Session {
|
||||
sessionid := ""
|
||||
if r, err := r.Cookie("gfsessionid"); err == nil {
|
||||
sessionid = r.Value
|
||||
} else {
|
||||
sessionid = gsession.Id()
|
||||
}
|
||||
return gsession.Get(sessionid)
|
||||
}
|
||||
|
||||
// 请求初始化时的回调函数
|
||||
func (c *Controller) __init(r *ClientRequest, w *ServerResponse) {
|
||||
|
||||
}
|
||||
|
||||
// 请求结束时的回调函数
|
||||
func (c *Controller) __shut(r *ClientRequest, w *ServerResponse) {
|
||||
|
||||
}
|
||||
@ -208,7 +208,7 @@ func (s *Server) handlerKey(domain, method, pattern string) string {
|
||||
func (s *Server) setHandler(domain, method, pattern string, handler HandlerFunc) {
|
||||
s.hmu.Lock()
|
||||
defer s.hmu.Unlock()
|
||||
if method == gDEFAULT_METHOD {
|
||||
if method == "all" {
|
||||
s.handlerMap[s.handlerKey(domain, "GET", pattern)] = handler
|
||||
s.handlerMap[s.handlerKey(domain, "PUT", pattern)] = handler
|
||||
s.handlerMap[s.handlerKey(domain, "POST", pattern)] = handler
|
||||
@ -241,9 +241,13 @@ func (s *Server)BindHandler(pattern string, handler HandlerFunc) error {
|
||||
if s.status == 1 {
|
||||
return errors.New("server handlers cannot be changed while running")
|
||||
}
|
||||
|
||||
s.hmu.Lock()
|
||||
defer s.hmu.Unlock()
|
||||
|
||||
uri := ""
|
||||
domain := gDEFAULT_DOMAIN
|
||||
method := gDEFAULT_METHOD
|
||||
method := "all"
|
||||
result := strings.Split(pattern, "@")
|
||||
if len(result) > 1 {
|
||||
domain = result[1]
|
||||
|
||||
94
g/net/gsession/gsession.go
Normal file
94
g/net/gsession/gsession.go
Normal file
@ -0,0 +1,94 @@
|
||||
package gsession
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"gitee.com/johng/gf/g/os/gtime"
|
||||
"gitee.com/johng/gf/g/util/grand"
|
||||
"gitee.com/johng/gf/g/container/gmap"
|
||||
"gitee.com/johng/gf/g/os/gcache"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_EXPIRE_TIME = 600 // 默认过期间隔(10分钟)
|
||||
)
|
||||
|
||||
// 单个session对象
|
||||
type Session struct {
|
||||
mu sync.RWMutex // 并发安全互斥锁
|
||||
id string // sessionid
|
||||
data *gmap.StringInterfaceMap // session数据
|
||||
expire int // 过期间隔(秒)
|
||||
}
|
||||
|
||||
// 生成一个唯一的sessionid字符串
|
||||
func Id() string {
|
||||
return strings.ToUpper(strconv.FormatInt(gtime.Nanosecond(), 32) + grand.RandStr(3))
|
||||
}
|
||||
|
||||
// 获取或者生成一个session对象
|
||||
func Get(sessionid string) *Session {
|
||||
if r := gcache.Get(cacheKey(sessionid)); r != nil {
|
||||
return r.(*Session)
|
||||
}
|
||||
s := &Session{
|
||||
data : gmap.NewStringInterfaceMap(),
|
||||
expire : DEFAULT_EXPIRE_TIME,
|
||||
}
|
||||
s.updateExpire()
|
||||
return s
|
||||
}
|
||||
|
||||
// session在gache中的缓存键名
|
||||
func cacheKey(sessionid string) string {
|
||||
return "session_" + sessionid
|
||||
}
|
||||
|
||||
// 获取sessionid
|
||||
func (s *Session) Id () string {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
id := s.id
|
||||
s.updateExpire()
|
||||
return id
|
||||
}
|
||||
|
||||
// 获取当前session所有数据
|
||||
func (s *Session) Data () map[string]interface{} {
|
||||
m := *s.data.Clone()
|
||||
s.updateExpire()
|
||||
return m
|
||||
}
|
||||
|
||||
// 设置session过期间隔
|
||||
func (s *Session) SetExpire (expire int) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.expire = expire
|
||||
s.updateExpire()
|
||||
}
|
||||
|
||||
// 设置session
|
||||
func (s *Session) Set (k string, v interface{}) {
|
||||
s.data.Set(k, v)
|
||||
s.updateExpire()
|
||||
}
|
||||
|
||||
// 获取session
|
||||
func (s *Session) Get (k string) interface{} {
|
||||
r := s.data.Get(k)
|
||||
s.updateExpire()
|
||||
return r
|
||||
}
|
||||
|
||||
// 删除session
|
||||
func (s *Session) Remove (k string) {
|
||||
s.data.Remove(k)
|
||||
s.updateExpire()
|
||||
}
|
||||
|
||||
// 更新过期时间
|
||||
func (s *Session) updateExpire() {
|
||||
gcache.Set(cacheKey(s.id), s, int64(s.expire*1000))
|
||||
}
|
||||
@ -2,10 +2,10 @@ package user
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g/frame/mvc"
|
||||
"html/template"
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/os/gfile"
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
// 定义业务相关的控制器对象
|
||||
@ -25,6 +25,7 @@ func init() {
|
||||
|
||||
// 定义操作逻辑
|
||||
func (cu *Controller_User) Info(r *ghttp.ClientRequest, w *ghttp.ServerResponse) {
|
||||
r.c
|
||||
//w.Write([]byte("user information page"))
|
||||
t, err := template.New("test").Funcs(template.FuncMap{"add": Add}).Parse(gfile.GetContents("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view/user/info.tpl"))
|
||||
if err != nil {
|
||||
|
||||
@ -3,6 +3,7 @@ import (
|
||||
"time"
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
"gitee.com/johng/gf/g/net/gsession"
|
||||
)
|
||||
type User struct {
|
||||
Username, Password string
|
||||
@ -12,6 +13,7 @@ func add(i1, i2 int) int {
|
||||
return i1 + i2 + 1
|
||||
}
|
||||
func main() {
|
||||
fmt.Println(gsession.Id())
|
||||
view := gmvc.NewView("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view/user/")
|
||||
tpl, _ := view.Template("info")
|
||||
tpl.BindFunc("add", add)
|
||||
|
||||
Reference in New Issue
Block a user