package runtimex import ( "os" "sync" ) type MapKV struct { kvMap map[interface{}]interface{} mu sync.RWMutex } func NewMapKV() MapKV { var mp MapKV mp.kvMap = make(map[interface{}]interface{}) return mp } func (m *MapKV) Get(key interface{}) (interface{}, error) { var err error m.mu.RLock() defer m.mu.RUnlock() data, ok := m.kvMap[key] if !ok { err = os.ErrNotExist } return data, err } func (m *MapKV) MustGet(key interface{}) interface{} { result, _ := m.Get(key) return result } func (m *MapKV) Store(key interface{}, value interface{}) error { m.mu.Lock() defer m.mu.Unlock() m.kvMap[key] = value return nil } func (m *MapKV) Exists(key interface{}) bool { m.mu.RLock() defer m.mu.RUnlock() _, ok := m.kvMap[key] return ok } func (m *MapKV) Delete(key interface{}) error { m.mu.Lock() defer m.mu.Unlock() delete(m.kvMap, key) return nil } func (m *MapKV) Range(run func(k interface{}, v interface{}) bool) error { for k, v := range m.kvMap { if !run(k, v) { break } } return nil }