package session import ( "errors" 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 } //mgr创建一个session 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 } func (s *MemorySessionMgr) GetSession(sessionId string) (session Session, err error) { s.rwLock.Lock() defer s.rwLock.Unlock() session, ok := s.sessionMap[sessionId] if !ok { err = errors.New("session not found") return } return }