gosora/common/menu_item_store.go

31 lines
577 B
Go
Raw Normal View History

package common
import "sync"
type DefaultMenuItemStore struct {
2022-02-21 03:32:53 +00:00
items map[int]MenuItem
lock sync.RWMutex
}
func NewDefaultMenuItemStore() *DefaultMenuItemStore {
2022-02-21 03:32:53 +00:00
return &DefaultMenuItemStore{
items: make(map[int]MenuItem),
}
}
func (s *DefaultMenuItemStore) Add(i MenuItem) {
2022-02-21 03:32:53 +00:00
s.lock.Lock()
defer s.lock.Unlock()
s.items[i.ID] = i
}
func (s *DefaultMenuItemStore) Get(id int) (MenuItem, error) {
2022-02-21 03:32:53 +00:00
s.lock.RLock()
item, ok := s.items[id]
s.lock.RUnlock()
if ok {
return item, nil
}
return item, ErrNoRows
}