-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoresMap.go
53 lines (43 loc) · 854 Bytes
/
storesMap.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
package tokay
import (
"sync"
)
type (
// routeStore stores route paths and the corresponding handlers.
routeStore interface {
Add(key string, data interface{}) int
Get(key string, pvalues []string) (data interface{}, pnames []string)
String() string
}
storesMap struct {
sync.RWMutex
M map[string]routeStore
}
)
func newStoresMap() *storesMap {
return &storesMap{M: make(map[string]routeStore)}
}
func (m *storesMap) Set(key string, val routeStore) {
m.Lock()
m.M[key] = val
m.Unlock()
}
func (m *storesMap) Range(fn func(key string, value routeStore)) {
m.Lock()
for key, value := range m.M {
fn(key, value)
}
m.Unlock()
}
func (m *storesMap) Get(key string) routeStore {
m.RLock()
v := m.M[key]
m.RUnlock()
return v
}
func (m *storesMap) Count() int {
m.RLock()
count := len(m.M)
m.RUnlock()
return count
}