-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_memory.go
61 lines (46 loc) · 1013 Bytes
/
store_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package imagine
import (
"sync"
"time"
)
type MemoryStoreParams struct {
TTL time.Duration
}
// InMemoryStorage is a storage implementation that stores data in memory
// This should only be used for testing
type MemoryStore struct {
params *MemoryStoreParams
mu *sync.RWMutex
cache map[string][]byte
}
var _ Store = new(MemoryStore)
func NewInMemoryStorage(params MemoryStoreParams) Store {
return &MemoryStore{
params: ¶ms,
cache: make(map[string][]byte),
mu: new(sync.RWMutex),
}
}
func (m *MemoryStore) Set(key string, data []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
m.cache[key] = data
return nil
}
func (m *MemoryStore) Get(key string) ([]byte, bool, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if data, ok := m.cache[key]; ok {
return data, true, nil
}
return nil, false, nil
}
func (m *MemoryStore) Delete(key string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.cache, key)
return nil
}
func (m *MemoryStore) Close() error {
return nil
}