-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsafe_map.go
57 lines (49 loc) · 988 Bytes
/
safe_map.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
/*
* 纸喵软件
* Copyright (c) 2017~2020 http://zhimiao.org All rights reserved.
* Author: 倒霉狐狸 <mail@xiaoliu.org>
* Date: 2020/3/3 下午4:26
*/
package gutils
import "sync"
// SafeStringMap 安全map
type SafeStringMap struct {
sync.RWMutex
Map map[string]string
}
// NewSafeStringMap 创建
func NewSafeStringMap() *SafeStringMap {
st := new(SafeStringMap)
st.Map = make(map[string]string)
return st
}
// GET 获取
func (st *SafeStringMap) GET(key string) string {
st.RLock()
value := st.Map[key]
st.RUnlock()
return value
}
// SET 设置
func (st *SafeStringMap) SET(key string, value string) {
st.Lock()
st.Map[key] = value
st.Unlock()
}
// SETNX map锁
func (st *SafeStringMap) SETNX(key string, value string) (ok bool) {
ok = false
st.Lock()
if _, ok = st.Map[key]; !ok {
st.Map[key] = value
ok = true
}
st.Unlock()
return ok
}
// DEL 删除
func (st *SafeStringMap) DEL(key string) {
st.Lock()
delete(st.Map, key)
st.Unlock()
}