gin/session/memorysession_mgr.go

40 lines
738 B
Go

package session
import (
uuid "github.com/satori/go.uuid"
"sync"
)
//定义对象
type MemorySessionMgr struct {
sessionMap map[string]Session
rwLock sync.RWMutex
}
//构造函数
func NewMemorySessionMgr() *MemorySessionMgr {
sr := &MemorySessionMgr{
sessionMap: make(map[string]Session, 1024),
}
return sr
}
func (s *MemorySessionMgr) Init(addr string, options ...string) (err error) {
return
}
func (s *MemorySessionMgr) CreateSession() (session Session, err error) {
s.rwLock.Lock()
defer s.rwLock.Unlock()
//用uuid作为session id
sessionId := uuid.NewV4().String()
//创建一个session
memorySession := NewMemorySession(sessionId)
s.sessionMap[sessionId] = memorySession
return memorySession, nil
}