/
ttlmap.go
94 lines (83 loc) · 1.76 KB
/
ttlmap.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gateway
// Credit to https://stackoverflow.com/questions/25484122/map-with-ttl-option-in-go
import (
"errors"
"sync"
"time"
model "github.com/ekotlikoff/gopoker/internal/model/table"
)
type item struct {
value *model.Player
lastAccess int64
}
// TTLMap is a map with a TTL
type TTLMap struct {
m map[string]*item
l sync.Mutex
}
// NewTTLMap creates a new map
func NewTTLMap(ln int, maxTTL int, gcFrequencySecs int) (m *TTLMap) {
m = &TTLMap{m: make(map[string]*item, ln)}
go func() {
gcFrequency := time.Tick(time.Second * time.Duration(gcFrequencySecs))
for now := range gcFrequency {
m.l.Lock()
for k, v := range m.m {
if now.Unix()-v.lastAccess > int64(maxTTL) {
delete(m.m, k)
}
}
m.l.Unlock()
}
}()
return
}
// Len returns the length of the map
func (m *TTLMap) Len() int {
m.l.Lock()
defer m.l.Unlock()
return len(m.m)
}
// Put puts key k and value v
func (m *TTLMap) Put(k string, v *model.Player) error {
m.l.Lock()
_, ok := m.m[k]
var it item
if !ok {
it := &item{value: v}
m.m[k] = it
} else {
return errors.New("failed to put key: " + k + ", value: " + v.Name)
}
it.lastAccess = time.Now().Unix()
m.l.Unlock()
return nil
}
// Get gets value for key k
func (m *TTLMap) Get(k string) (v *model.Player, err error) {
m.l.Lock()
if it, ok := m.m[k]; ok {
v = it.value
it.lastAccess = time.Now().Unix()
} else {
err = errors.New("failed to get")
}
m.l.Unlock()
return
}
// Refresh updates the key k to newk
func (m *TTLMap) Refresh(k, newk string) error {
m.l.Lock()
it, ok := m.m[k]
if ok {
it.lastAccess = time.Now().Unix()
}
if _, newok := m.m[newk]; !newok && ok {
m.m[newk] = it
delete(m.m, k)
} else {
return errors.New("failed to refresh key")
}
m.l.Unlock()
return nil
}