-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
75 lines (66 loc) · 1.89 KB
/
context.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
//by yyrdl ,MIT License ,welcome to use and welcome to star it :)
package gbeta
import (
"sync"
)
//context stores values of url parameters ,and maybe middleware will also set
// some key-value in it . Context can be passed to functions running in different
//goroutines.Contexts are safe for simultaneous use by multiple goroutines.
//Please just pass the pointer (*Context), do not copy it
//context 用来存放url参数,中间件也可以在上面设置信息,比如json-parser可以将json的解析结果放在里面
//context是线程安全的,但一定要以指针的方式进行传递
type Context struct {
mu *sync.RWMutex
store map[interface{}]interface{}
}
//设置一个键值对
//set a key-value
func (c *Context) Set(key, value interface{}) {
c.mu.Lock()
c.store[key] = value
c.mu.Unlock()
}
//获取key对应的value
// get the value of corresponding key
func (c *Context) Get(key interface{}) interface{} {
c.mu.RLock()
v := c.store[key]
c.mu.RUnlock()
return v
}
//删除某一个键值对
//delete a key-value pair
func (c *Context) Delete(key interface{}) {
c.mu.Lock()
delete(c.store, key)
c.mu.Unlock()
}
//按用户定义的check规则检查某一个值,如果返回true,则对应的key ,value添加进去
//if the checkFunc return true ,the value will be stored
func (c *Context) CheckAndSet(key, value interface{}, checkFunc CheckFunc) bool {
is_successful := false
c.mu.Lock()
v := c.store[key]
if checkFunc(v, value) {
c.store[key] = value
is_successful = true
}
c.mu.Unlock()
return is_successful
}
//清空map ,以便放回sync.pool
//clear the key-value that are stored in the map
func (c *Context) Clear() {
c.mu.Lock()
for key, _ := range c.store {
delete(c.store, key)
}
c.mu.Unlock()
}
//create a new Context
func NewContext() *Context {
c := new(Context)
c.mu = new(sync.RWMutex)
c.store = make(map[interface{}]interface{}, 5)
return c
}