-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmap.go
More file actions
73 lines (60 loc) · 1.12 KB
/
cmap.go
File metadata and controls
73 lines (60 loc) · 1.12 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
package cmap
import (
"sync"
)
type CMap struct {
v map[string]interface{}
l sync.RWMutex
afterSet CbAfterSet
afterGet CbAfterGet
}
type MapItem struct {
Key string
Value interface{}
}
type CbAfterSet func()
type CbAfterGet func()
func NewCMap() *CMap {
return &CMap{v: make(map[string]interface{})}
}
func (m *CMap) SetListenerSet(listener CbAfterSet) {
m.afterSet = listener
}
func (m *CMap) SetListenerGet(listener CbAfterGet) {
m.afterGet = listener
}
func (m *CMap) Size() int {
return len(m.v)
}
func (m *CMap) Set(key string, value interface{}) error {
m.l.Lock()
defer m.l.Unlock()
m.v[key] = value
if m.afterSet != nil {
m.afterSet()
}
return nil
}
func (m *CMap) Get(key string) (value interface{}, exists bool) {
m.l.RLock()
defer m.l.RUnlock()
value, exists = m.v[key]
return
}
func (m *CMap) Exists(key string) bool {
m.l.RLock()
defer m.l.RUnlock()
_, exists := m.v[key]
return exists
}
func (m *CMap) Dump() <-chan MapItem {
outChan := make(chan MapItem, m.Size())
for k, v := range m.v {
outChan <- MapItem{
Key: k,
Value: v,
}
}
close(outChan)
return outChan
}