-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathdata.go
More file actions
129 lines (119 loc) · 2.2 KB
/
data.go
File metadata and controls
129 lines (119 loc) · 2.2 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package session
import (
"sync"
)
// msgp -file="data.go" -o="data_msgp.go" -tests=true -unexported
// Session state should remain small to fit common storage payload limits.
//
//go:generate msgp -o=data_msgp.go -tests=true -unexported
//msgp:ignore data
type data struct {
Data map[any]any // Session key counts are expected to be bounded.
sync.RWMutex `msg:"-"`
}
var dataPool = sync.Pool{
New: func() any {
d := new(data)
d.Data = make(map[any]any)
return d
},
}
// acquireData returns a new data object from the pool.
//
// Returns:
// - *data: The data object.
//
// Usage:
//
// d := acquireData()
func acquireData() *data {
d, ok := dataPool.Get().(*data)
if !ok {
d = new(data)
d.Data = make(map[any]any)
}
return d
}
// Reset clears the data map and resets the data object.
//
// Usage:
//
// d.Reset()
func (d *data) Reset() {
d.Lock()
defer d.Unlock()
clear(d.Data)
}
// Get retrieves a value from the data map by key.
//
// Parameters:
// - key: The key to retrieve.
//
// Returns:
// - any: The value associated with the key.
//
// Usage:
//
// value := d.Get("key")
func (d *data) Get(key any) any {
d.RLock()
defer d.RUnlock()
return d.Data[key]
}
// Set updates or creates a new key-value pair in the data map.
//
// Parameters:
// - key: The key to set.
// - value: The value to set.
//
// Usage:
//
// d.Set("key", "value")
func (d *data) Set(key, value any) {
d.Lock()
defer d.Unlock()
d.Data[key] = value
}
// Delete removes a key-value pair from the data map.
//
// Parameters:
// - key: The key to delete.
//
// Usage:
//
// d.Delete("key")
func (d *data) Delete(key any) {
d.Lock()
defer d.Unlock()
delete(d.Data, key)
}
// Keys retrieves all keys in the data map.
//
// Returns:
// - []any: A slice of all keys in the data map.
//
// Usage:
//
// keys := d.Keys()
func (d *data) Keys() []any {
d.RLock()
defer d.RUnlock()
keys := make([]any, 0, len(d.Data))
for k := range d.Data {
keys = append(keys, k)
}
return keys
}
// Len returns the number of key-value pairs in the data map.
//
// Returns:
// - int: The number of key-value pairs.
//
// Usage:
//
// length := d.Len()
func (d *data) Len() int {
d.RLock()
defer d.RUnlock()
return len(d.Data)
}