-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_store.go
59 lines (50 loc) · 1.08 KB
/
a_store.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
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import "sync"
type StorageEngine interface {
Get(key string) (string, error)
Set(key, value string) error
Delete(key string) error
Snapshot() (map[string]string, error)
Restore(o map[string]string) error
}
type MemStorageEngine struct {
mu sync.Mutex
data map[string]string
}
func NewMemStorageEngine() StorageEngine {
return &MemStorageEngine{
data: make(map[string]string),
}
}
func (s *MemStorageEngine) Get(key string) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.data[key], nil
}
func (s *MemStorageEngine) Set(key, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.data[key] = value
return nil
}
func (s *MemStorageEngine) Delete(key string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.data, key)
return nil
}
func (s *MemStorageEngine) Snapshot() (map[string]string, error) {
s.mu.Lock()
defer s.mu.Unlock()
o := make(map[string]string)
for k, v := range s.data {
o[k] = v
}
return o, nil
}
func (s *MemStorageEngine) Restore(o map[string]string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.data = o
return nil
}