diff --git a/go.mod b/go.mod index 38389b4..7e6bf86 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/satori/go.uuid v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.10.0 // indirect diff --git a/go.sum b/go.sum index 1de6a82..0a17442 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/session/memory.go b/session/memory.go new file mode 100644 index 0000000..b54948c --- /dev/null +++ b/session/memory.go @@ -0,0 +1,57 @@ +package session + +import ( + "errors" + "sync" +) + +//对象 + +type MemorySession struct { + sessionId string + //存kv + data map[string]interface{} + rwLock sync.RWMutex +} + +//构造函数 + +func NewMemorySession(id string) *MemorySession { + s := &MemorySession{ + sessionId: id, + data: make(map[string]interface{}, 16), + } + return s +} + +//增删改查 + +func (m *MemorySession) Set(key string, value interface{}) (err error) { + //枷锁 + m.rwLock.Lock() + defer m.rwLock.Unlock() + m.data[key] = value + return +} + +func (m *MemorySession) Get(key string) (value interface{}, err error) { + m.rwLock.Lock() + defer m.rwLock.Unlock() + value, ok := m.data[key] + if !ok { + err = errors.New("key not exist in session") + return + } + return +} + +func (m *MemorySession) Del(key string) (err error) { + m.rwLock.Lock() + defer m.rwLock.Unlock() + delete(m.data, key) + return +} + +func (m *MemorySession) Save() (err error) { + return +} diff --git a/session/memorysession_mgr.go b/session/memorysession_mgr.go new file mode 100644 index 0000000..3748d97 --- /dev/null +++ b/session/memorysession_mgr.go @@ -0,0 +1,39 @@ +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 + +}