Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-05-03 19:14:51 +08:00
parent 6a7a41b634
commit 5b57025f80

View File

@ -53,8 +53,6 @@ type Context struct {
engine *Engine
EnableMutexLock bool
// This mutex protect Keys map
mu sync.RWMutex
@ -235,27 +233,21 @@ func (c *Context) Error(err error) *Error {
// Set is used to store a new key/value pair exclusively for this context.
// It also lazy initializes c.Keys if it was not used previously.
func (c *Context) Set(key string, value interface{}) {
if c.EnableMutexLock {
c.mu.Lock()
defer c.mu.Unlock()
}
c.mu.Lock()
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.Keys[key] = value
c.mu.Unlock()
}
// Get returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
func (c *Context) Get(key string) (value interface{}, exists bool) {
if c.EnableMutexLock {
c.mu.RLock()
defer c.mu.RUnlock()
}
c.mu.RLock()
value, exists = c.Keys[key]
c.mu.RUnlock()
return
}