liujiandao
2024-03-26 1c17ff16fd13e4d8bbab75d8a728cf18465b20e0
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
package service
 
import (
    "github.com/hashicorp/golang-lru/v2/expirable"
    "strings"
    "time"
)
 
type UserBaseInfo struct {
    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*1) //todo zq 暂时改成 1分钟
}
 
func GetUserBaseCache(adminUserId string) *UserBaseInfo {
    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,
            SubUserIds: subIds,
        }
        SetUserBaseCache(adminUserId, baseInfo)
        return baseInfo
    }
    return cache
}
 
func SetUserBaseCache(adminUserId string, user *UserBaseInfo) {
    _ = userCache.Add(adminUserId, user)
}