gin/session/memorysession_mgr.go

52 lines
995 B
Go
Raw Permalink Normal View History

2024-09-19 17:55:12 +08:00
package session
import (
2024-09-24 21:18:05 +08:00
"errors"
2024-09-19 17:55:12 +08:00
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
}
2024-09-24 21:18:05 +08:00
//mgr创建一个session
2024-09-19 17:55:12 +08:00
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
2024-09-24 21:18:05 +08:00
return
}
2024-09-19 17:55:12 +08:00
2024-09-24 21:18:05 +08:00
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
2024-09-19 17:55:12 +08:00
}