-
Notifications
You must be signed in to change notification settings - Fork 214
/
role_mgr.go
50 lines (41 loc) · 864 Bytes
/
role_mgr.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
package mgr
import (
"sync"
"github.com/llr104/slgserver/db"
"github.com/llr104/slgserver/log"
"github.com/llr104/slgserver/server/slgserver/model"
"go.uber.org/zap"
)
func RoleNickName(rid int) string {
vRole, ok := RMgr.Get(rid)
if ok {
return vRole.NickName
}
return ""
}
type roleMgr struct {
mutex sync.RWMutex
roles map[int]*model.Role
}
var RMgr = &roleMgr{
roles: make(map[int]*model.Role),
}
func (this*roleMgr) Get(rid int) (*model.Role, bool){
this.mutex.RLock()
r, ok := this.roles[rid]
this.mutex.RUnlock()
if ok {
return r, true
}
m := &model.Role{}
ok, err := db.MasterDB.Table(new(model.Role)).Where("rid=?", rid).Get(m)
if ok {
this.mutex.Lock()
this.roles[rid] = m
this.mutex.Unlock()
return m, true
}else{
log.DefaultLog.Warn("db error", zap.Error(err), zap.Int("rid", rid))
return nil, false
}
}