chitchat/server/storage/storage_mem.go

63 lines
917 B
Go
Raw Normal View History

2025-01-05 16:28:42 +00:00
package storage
import "sync"
func NewMemStore() Store {
s := &memStore{
elements: make(map[string][]byte, 0),
lock: sync.Mutex{},
}
return s
}
type memStore struct {
elements map[string][]byte
lock sync.Mutex
}
func (s *memStore) Has(id string) error {
s.lock.Lock()
defer s.lock.Unlock()
_, found := s.elements[id]
if !found {
return ErrNotExists
}
return nil
}
func (s *memStore) Get(id string) ([]byte, error) {
s.lock.Lock()
defer s.lock.Unlock()
element, found := s.elements[id]
if !found {
return nil, ErrNotExists
}
return element, nil
}
func (s *memStore) Put(id string, element []byte) error {
s.lock.Lock()
defer s.lock.Unlock()
s.elements[id] = element
return nil
}
func (s *memStore) Delete(id string) error {
s.lock.Lock()
defer s.lock.Unlock()
_, found := s.elements[id]
if !found {
return ErrNotExists
}
delete(s.elements, id)
return nil
}