-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathin-memory-log.go
More file actions
62 lines (46 loc) · 1 KB
/
in-memory-log.go
File metadata and controls
62 lines (46 loc) · 1 KB
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
60
61
62
package stores
import (
"context"
"fmt"
"sync"
)
type inMemoryStore struct {
mu sync.RWMutex
values map[string]string
}
func NewInMemoryStore() Store {
return &inMemoryStore{
values: make(map[string]string),
}
}
func (s *inMemoryStore) Set(ctx context.Context, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.values[key] = value
return nil
}
func (s *inMemoryStore) Get(ctx context.Context, key string) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
v, ok := s.values[key]
if !ok {
return "", fmt.Errorf("value for key '%s' not found", key)
}
return v, nil
}
func (s *inMemoryStore) Delete(ctx context.Context, key string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.values, key)
return nil
}
func (s *inMemoryStore) Keys(ctx context.Context) ([]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var result []string
for k := range s.values {
result = append(result, k)
}
return result, nil
}
func (s *inMemoryStore) Release(context.Context) {}