forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
63 lines (53 loc) · 1022 Bytes
/
data.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
package session
import (
"sync"
)
// go:generate msgp
// msgp -file="data.go" -o="data_msgp.go" -tests=false -unexported
type data struct {
sync.RWMutex
Data map[string]interface{}
}
var dataPool = sync.Pool{
New: func() interface{} {
d := new(data)
d.Data = make(map[string]interface{})
return d
},
}
func acquireData() *data {
return dataPool.Get().(*data) //nolint:forcetypeassert // We store nothing else in the pool
}
func (d *data) Reset() {
d.Lock()
d.Data = make(map[string]interface{})
d.Unlock()
}
func (d *data) Get(key string) interface{} {
d.RLock()
v := d.Data[key]
d.RUnlock()
return v
}
func (d *data) Set(key string, value interface{}) {
d.Lock()
d.Data[key] = value
d.Unlock()
}
func (d *data) Delete(key string) {
d.Lock()
delete(d.Data, key)
d.Unlock()
}
func (d *data) Keys() []string {
d.Lock()
keys := make([]string, 0, len(d.Data))
for k := range d.Data {
keys = append(keys, k)
}
d.Unlock()
return keys
}
func (d *data) Len() int {
return len(d.Data)
}