/
storage_memory.go
46 lines (33 loc) · 909 Bytes
/
storage_memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package appserver
import "sync"
var _ CredentialStore = (*MemoryCredentialStore)(nil)
type MemoryCredentialStore struct {
credentials map[string]*Credentials
mapMu sync.Mutex
}
func NewMemoryCredentialStore() CredentialStore {
return &MemoryCredentialStore{
credentials: make(map[string]*Credentials),
}
}
func (m *MemoryCredentialStore) Store(credentials *Credentials) error {
m.mapMu.Lock()
defer m.mapMu.Unlock()
m.credentials[credentials.ShopID] = credentials
return nil
}
func (m *MemoryCredentialStore) Get(shopID string) (*Credentials, error) {
if cred, ok := m.credentials[shopID]; ok {
return cred, nil
}
return nil, ErrCredentialsNotFound
}
func (m *MemoryCredentialStore) Delete(shopID string) error {
if _, ok := m.credentials[shopID]; !ok {
return ErrCredentialsNotFound
}
m.mapMu.Lock()
defer m.mapMu.Unlock()
delete(m.credentials, shopID)
return nil
}