| | |
| | | |
| | | import ( |
| | | "github.com/hashicorp/golang-lru/v2/expirable" |
| | | "strings" |
| | | "time" |
| | | ) |
| | | |
| | | type UserBaseInfo struct { |
| | | UserId int |
| | | NickName string |
| | | UserId int |
| | | NickName string |
| | | SubUserIds []int |
| | | } |
| | | |
| | | var userCache *expirable.LRU[string, *UserBaseInfo] |
| | | |
| | | func init() { |
| | | //make cache with 5 minutes TTL and 100 max keys |
| | | userCache = expirable.NewLRU[string, *UserBaseInfo](100, nil, time.Minute*5) |
| | | userCache = expirable.NewLRU[string, *UserBaseInfo](100, nil, time.Minute*1) //todo zq 暂时改成 1分钟 |
| | | } |
| | | |
| | | func GetUserBaseCache(adminUserId string) *UserBaseInfo { |
| | | userCache, ok := userCache.Get(adminUserId) |
| | | cache, ok := userCache.Get(adminUserId) |
| | | if !ok { |
| | | userService := UserService{} |
| | | userRecord, err := userService.GetUserInfo(adminUserId) |
| | | if err != nil { |
| | | return nil |
| | | } |
| | | var subIds []int |
| | | if userRecord.SubUserIds != "" { |
| | | subIds, _, err = userService.UUID2CrmUserId(strings.Split(userRecord.SubUserIds, ",")) |
| | | if err != nil { |
| | | return nil |
| | | } |
| | | } |
| | | |
| | | subIds = append(subIds, userRecord.ID) |
| | | |
| | | baseInfo := &UserBaseInfo{ |
| | | UserId: userRecord.ID, |
| | | NickName: userRecord.NickName, |
| | | UserId: userRecord.ID, |
| | | NickName: userRecord.NickName, |
| | | SubUserIds: subIds, |
| | | } |
| | | SetUserBaseCache(adminUserId, baseInfo) |
| | | return baseInfo |
| | | } |
| | | return userCache |
| | | return cache |
| | | } |
| | | |
| | | func SetUserBaseCache(adminUserId string, user *UserBaseInfo) { |