package user_c import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" "sync" "time" ) type UserLevel struct { UserId mysql.ID Level int } var svipVipNobleLock sync.RWMutex var svipLevel = make(map[mysql.ID]int) // userId->svipLevel var nobleLevel = make(map[mysql.ID]uint16) // userId->nobleLevel var userVips = make(map[mysql.ID]bool) // userId->isVip // 加载数据到lru func LoadAllSvipVipNoble(model *domain.Model) { start := time.Now() var svips []UserLevel svipMap := make(map[mysql.ID]int) if err := model.DB().Table("user_svip").Select("user_id,level").Where("level > 0").Find(&svips).Error; err != nil { model.Log.Errorf("LoadAllSvip fail:%v", err) } else { for _, v := range svips { svipMap[v.UserId] = v.Level } } var nobles []UserLevel nobleMap := make(map[mysql.ID]uint16) if err := model.DB().Table("user_noble").Select("user_id,MAX(level) level"). Where("level > 0 AND end_time > ?", time.Now()).Group("user_id").Find(&nobles).Error; err != nil { model.Log.Errorf("LoadAllNoble fail:%v", err) } else { for _, v := range nobles { nobleMap[v.UserId] = uint16(v.Level) } } var vips []UserLevel vipMap := make(map[mysql.ID]bool) if err := model.DB().Table("user_vip").Select("user_id"). Where("expire_at > ?", time.Now()).Find(&vips).Error; err != nil { model.Log.Errorf("LoadAllVip fail:%v", err) } else { for _, v := range vips { vipMap[v.UserId] = true } } // 上锁赋值 svipVipNobleLock.Lock() svipLevel = svipMap nobleLevel = nobleMap userVips = vipMap svipVipNobleLock.Unlock() model.Log.Infof("LoadAllSvipVipNoble svip:%v,vip:%v,noble:%v,cost:%vs", len(svipMap), len(vipMap), len(nobleMap), time.Now().Sub(start).Seconds()) } func BatchGetNobleLevel(model *domain.Model, userIds []uint64) map[uint64]uint16 { res := make(map[uint64]uint16) svipVipNobleLock.RLock() defer svipVipNobleLock.RUnlock() for _, userId := range userIds { res[userId] = nobleLevel[userId] } return res } func MGetUserSvipLevel(model *domain.Model, userIds []uint64) map[uint64]int { res := make(map[uint64]int) svipVipNobleLock.RLock() defer svipVipNobleLock.RUnlock() for _, userId := range userIds { res[userId] = svipLevel[userId] } return res } func BatchGetVips(model *domain.Model, userIds []uint64) map[uint64]*int64 { res := make(map[uint64]*int64) svipVipNobleLock.RLock() defer svipVipNobleLock.RUnlock() for _, userId := range userIds { vip := int64(1) if userVips[userId] { res[userId] = &vip } } return res }