forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrenceMap.go
More file actions
157 lines (139 loc) · 3.66 KB
/
concurrenceMap.go
File metadata and controls
157 lines (139 loc) · 3.66 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package core
import (
"fmt"
"sync"
"time"
)
type (
// ReadonlyMap only support readonly method for map
ReadonlyMap interface {
Get(key string) (value interface{}, exists bool)
GetString(key string) string
GetTimeDuration(key string) time.Duration
GetInt(key string) int
GetUInt64(key string) uint64
Exists(key string) bool
Len() int
}
// ReadonlyMap support concurrence for map
ConcurrenceMap interface {
Get(key string) (value interface{}, exists bool)
GetString(key string) string
GetTimeDuration(key string) time.Duration
GetInt(key string) int
GetUInt64(key string) uint64
Exists(key string) bool
GetCurrentMap() map[string]interface{}
Len() int
Set(key string, value interface{}) error
Remove(key string)
Once(key string) (value interface{}, exists bool)
}
)
// ItemMap concurrence map
type ItemMap struct {
innerMap map[string]interface{}
*sync.RWMutex
}
// NewItemMap create new ItemMap
func NewItemMap() *ItemMap {
return &ItemMap{
innerMap: make(map[string]interface{}),
RWMutex: new(sync.RWMutex),
}
}
// NewConcurrenceMap create new ConcurrenceMap
func NewConcurrenceMap() ConcurrenceMap {
return &ItemMap{
innerMap: make(map[string]interface{}),
RWMutex: new(sync.RWMutex),
}
}
// NewReadonlyMap create new ReadonlyMap
func NewReadonlyMap() ReadonlyMap {
return &ItemMap{
innerMap: make(map[string]interface{}),
RWMutex: new(sync.RWMutex),
}
}
// Set 以key、value置入ItemMap
func (ctx *ItemMap) Set(key string, value interface{}) error {
ctx.Lock()
ctx.innerMap[key] = value
ctx.Unlock()
return nil
}
// Get 读取指定key在ItemMap中的内容
func (ctx *ItemMap) Get(key string) (value interface{}, exists bool) {
ctx.RLock()
value, exists = ctx.innerMap[key]
ctx.RUnlock()
return value, exists
}
// Remove remove item by gived key
// if not exists key, do nothing...
func (ctx *ItemMap) Remove(key string) {
ctx.Lock()
delete(ctx.innerMap, key)
ctx.Unlock()
}
// Once get item by gived key, and remove it
// only can be read once, it will be locked
func (ctx *ItemMap) Once(key string) (value interface{}, exists bool) {
ctx.Lock()
defer ctx.Unlock()
value, exists = ctx.innerMap[key]
if exists {
delete(ctx.innerMap, key)
}
return value, exists
}
// GetString 读取指定key在ConcurrenceMap中的内容,以string格式输出
// 如果不存在key,返回空字符串
func (ctx *ItemMap) GetString(key string) string {
value, exists := ctx.Get(key)
if !exists {
return ""
}
return fmt.Sprint(value)
}
// GetInt 读取指定key在ConcurrenceMap中的内容,以int格式输出
// 如果不存在key,或者转换失败,返回0
func (ctx *ItemMap) GetInt(key string) int {
value, exists := ctx.Get(key)
if !exists {
return 0
}
return value.(int)
}
// GetUInt64 读取指定key在ConcurrenceMap中的内容,以uint64格式输出
// 如果不存在key,或者转换失败,返回0
func (ctx *ItemMap) GetUInt64(key string) uint64 {
value, exists := ctx.Get(key)
if !exists {
return 0
}
return value.(uint64)
}
// GetTimeDuration 读取指定key在ConcurrenceMap中的内容,以time.Duration格式输出
// 如果不存在key,或者转换失败,返回0
func (ctx *ItemMap) GetTimeDuration(key string) time.Duration {
timeDuration, err := time.ParseDuration(ctx.GetString(key))
if err != nil {
return 0
}
return timeDuration
}
// Exists check exists key
func (ctx *ItemMap) Exists(key string) bool {
_, exists := ctx.innerMap[key]
return exists
}
// GetCurrentMap get current map, returns map[string]interface{}
func (ctx *ItemMap) GetCurrentMap() map[string]interface{} {
return ctx.innerMap
}
// Len get context length
func (ctx *ItemMap) Len() int {
return len(ctx.innerMap)
}