-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
62 lines (52 loc) · 979 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
package session
import (
"sync"
)
// go:generate msgp
// msgp -file="data.go" -o="data_msgp.go" -tests=false -unexported
// don't forget to replace the msgp import path to:
// "github.com/yonzay/tlsfiber/internal/msgp"
type data struct {
sync.RWMutex `gotiny:"-"`
d map[string]interface{} `gotiny:"d"`
}
var dataPool = sync.Pool{
New: func() interface{} {
d := new(data)
d.d = make(map[string]interface{})
return d
},
}
func acquireData() *data {
return dataPool.Get().(*data)
}
func releaseData(d *data) {
d.Reset()
dataPool.Put(d)
}
func (d *data) Reset() {
d.Lock()
for key := range d.d {
delete(d.d, key)
}
d.Unlock()
}
func (d *data) Get(key string) interface{} {
d.RLock()
v := d.d[key]
d.RUnlock()
return v
}
func (d *data) Set(key string, value interface{}) {
d.Lock()
d.d[key] = value
d.Unlock()
}
func (d *data) Delete(key string) {
d.Lock()
delete(d.d, key)
d.Unlock()
}
func (d *data) Len() int {
return len(d.d)
}