-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_memory.go
77 lines (66 loc) · 1.49 KB
/
in_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cache
import (
"sync"
"time"
)
// InMem is a struct which implements Storage interface using
// memory as storage
type InMem struct {
data map[string]interface{}
expire map[string]time.Time
done chan struct{}
sync.RWMutex
}
// InMemory creates a new in memory storage which can be passed to
// cache.New(WithStorage(...))
func InMemory() *InMem {
inMemory := &InMem{
data: make(map[string]interface{}),
expire: make(map[string]time.Time),
}
return inMemory
}
// Write writes the given content for the given key in
// memory storage
func (i *InMem) Write(key string, v interface{}, d time.Duration) error {
i.Lock()
defer i.Unlock()
i.data[key] = v
if d != 0 {
i.expire[key] = time.Now().Add(d)
}
return nil
}
// Read reads coontent for the given key from in memory storage
func (i *InMem) Read(key string) (interface{}, error) {
i.RLock()
defer i.RUnlock()
v, ok := i.data[key]
if !ok {
return nil, ErrKeyNotExist
}
expire, ok := i.expire[key]
if ok && expire.Before(time.Now()) {
i.del(key)
return nil, ErrKeyNotExist
}
return v, nil
}
// Delete deletes content of the given key from in memory storage
func (i *InMem) Delete(key string) error {
i.Lock()
defer i.Unlock()
err := i.del(key)
return err
}
func (i *InMem) del(key string) error {
delete(i.data, key)
delete(i.expire, key)
return nil
}
// Flush flushes in momory storage
func (i *InMem) Flush() error {
i.data = make(map[string]interface{})
i.expire = make(map[string]time.Time)
return nil
}