-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.go
76 lines (67 loc) · 1.24 KB
/
store.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
package plex
import (
"sync"
)
// connStore
type connStore struct {
// client data
data map[string]*storeInfo
// lock
rwMu sync.RWMutex
}
// newConnStore
func newConnStore(capacity ...int) *connStore {
var (
cap int
)
if len(capacity) > 0 {
cap = capacity[0]
}
return &connStore{
data: make(map[string]*storeInfo, cap),
}
}
// get
func (cs *connStore) get(key string) (value *storeInfo, exists bool) {
cs.rwMu.RLock()
value, exists = cs.data[key]
cs.rwMu.RUnlock()
return value, exists
}
// set
func (cs *connStore) set(key string, value *storeInfo) {
cs.rwMu.Lock()
cs.data[key] = value
cs.rwMu.Unlock()
}
// getOrSet if
func (cs *connStore) getOrSet(key string, value *storeInfo) (actual *storeInfo, exists bool) {
cs.rwMu.Lock()
actual, exists = cs.data[key]
if !exists {
cs.data[key] = value
actual = value
}
cs.rwMu.Unlock()
return actual, exists
}
// del
func (cs *connStore) del(key string) {
cs.rwMu.Lock()
delete(cs.data, key)
cs.rwMu.Unlock()
}
// clear clears all data
func (cs *connStore) clear() {
cs.rwMu.Lock()
for k := range cs.data {
delete(cs.data, k)
}
cs.rwMu.Unlock()
}
// len return data length
func (cs *connStore) len() int {
cs.rwMu.RLock()
defer cs.rwMu.RUnlock()
return len(cs.data)
}