github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/menu_item_store.go (about)

     1  package common
     2  
     3  import "sync"
     4  
     5  type DefaultMenuItemStore struct {
     6  	items map[int]MenuItem
     7  	lock  sync.RWMutex
     8  }
     9  
    10  func NewDefaultMenuItemStore() *DefaultMenuItemStore {
    11  	return &DefaultMenuItemStore{
    12  		items: make(map[int]MenuItem),
    13  	}
    14  }
    15  
    16  func (s *DefaultMenuItemStore) Add(i MenuItem) {
    17  	s.lock.Lock()
    18  	defer s.lock.Unlock()
    19  	s.items[i.ID] = i
    20  }
    21  
    22  func (s *DefaultMenuItemStore) Get(id int) (MenuItem, error) {
    23  	s.lock.RLock()
    24  	item, ok := s.items[id]
    25  	s.lock.RUnlock()
    26  	if ok {
    27  		return item, nil
    28  	}
    29  	return item, ErrNoRows
    30  }