day 14 finished
parent
2a7ff7a1ba
commit
b89259a895
1
go.mod
1
go.mod
|
@ -21,6 +21,7 @@ require (
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.3 // 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/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
golang.org/x/arch v0.10.0 // indirect
|
golang.org/x/arch v0.10.0 // indirect
|
||||||
|
|
2
go.sum
2
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 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
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/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.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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue