gin/session/memory.go

58 lines
912 B
Go
Raw Normal View History

2024-09-19 17:55:12 +08:00
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
}