package session import "sync" type Store interface { Load(key string) (value NetConn, exist bool) Delete(key string) Store(key string, value NetConn) Range(f func(key string, value NetConn) bool) } func NewMapStore() Store { return &mapStore{ data: make(map[string]NetConn), } } type mapStore struct { sync.RWMutex data map[string]NetConn } func (c *mapStore) Len() int { c.RLock() defer c.RUnlock() return len(c.data) } func (c *mapStore) Load(key string) (value NetConn, exist bool) { c.RLock() defer c.RUnlock() value, exist = c.data[key] return } func (c *mapStore) Delete(key string) { c.Lock() defer c.Unlock() delete(c.data, key) } func (c *mapStore) Store(key string, value NetConn) { c.Lock() defer c.Unlock() c.data[key] = value } func (c *mapStore) Range(f func(key string, value NetConn) bool) { c.RLock() defer c.RUnlock() for k, v := range c.data { if !f(k, v) { return } } }