|
| 1 | +package mcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache" |
| 11 | + "github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey" |
| 12 | + "github.com/openimsdk/open-im-server/v3/pkg/common/storage/database" |
| 13 | + "github.com/openimsdk/tools/errs" |
| 14 | + "github.com/openimsdk/tools/log" |
| 15 | +) |
| 16 | + |
| 17 | +func NewTokenCacheModel(cache database.Cache, accessExpire int64) cache.TokenModel { |
| 18 | + c := &tokenCache{cache: cache} |
| 19 | + c.accessExpire = c.getExpireTime(accessExpire) |
| 20 | + return c |
| 21 | +} |
| 22 | + |
| 23 | +type tokenCache struct { |
| 24 | + cache database.Cache |
| 25 | + accessExpire time.Duration |
| 26 | +} |
| 27 | + |
| 28 | +func (x *tokenCache) getTokenKey(userID string, platformID int, token string) string { |
| 29 | + return cachekey.GetTokenKey(userID, platformID) + ":" + token |
| 30 | +} |
| 31 | + |
| 32 | +func (x *tokenCache) SetTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error { |
| 33 | + return x.cache.Set(ctx, x.getTokenKey(userID, platformID, token), strconv.Itoa(flag), x.accessExpire) |
| 34 | +} |
| 35 | + |
| 36 | +// SetTokenFlagEx set token and flag with expire time |
| 37 | +func (x *tokenCache) SetTokenFlagEx(ctx context.Context, userID string, platformID int, token string, flag int) error { |
| 38 | + return x.SetTokenFlag(ctx, userID, platformID, token, flag) |
| 39 | +} |
| 40 | + |
| 41 | +func (x *tokenCache) GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error) { |
| 42 | + prefix := x.getTokenKey(userID, platformID, "") |
| 43 | + m, err := x.cache.Prefix(ctx, prefix) |
| 44 | + if err != nil { |
| 45 | + return nil, errs.Wrap(err) |
| 46 | + } |
| 47 | + mm := make(map[string]int) |
| 48 | + for k, v := range m { |
| 49 | + state, err := strconv.Atoi(v) |
| 50 | + if err != nil { |
| 51 | + log.ZError(ctx, "token value is not int", err, "value", v, "userID", userID, "platformID", platformID) |
| 52 | + continue |
| 53 | + } |
| 54 | + mm[strings.TrimPrefix(k, prefix)] = state |
| 55 | + } |
| 56 | + return mm, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (x *tokenCache) HasTemporaryToken(ctx context.Context, userID string, platformID int, token string) error { |
| 60 | + key := cachekey.GetTemporaryTokenKey(userID, platformID, token) |
| 61 | + if _, err := x.cache.Get(ctx, []string{key}); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (x *tokenCache) GetAllTokensWithoutError(ctx context.Context, userID string) (map[int]map[string]int, error) { |
| 68 | + prefix := cachekey.UidPidToken + userID + ":" |
| 69 | + tokens, err := x.cache.Prefix(ctx, prefix) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + res := make(map[int]map[string]int) |
| 74 | + for key, flagStr := range tokens { |
| 75 | + flag, err := strconv.Atoi(flagStr) |
| 76 | + if err != nil { |
| 77 | + log.ZError(ctx, "token value is not int", err, "key", key, "value", flagStr, "userID", userID) |
| 78 | + continue |
| 79 | + } |
| 80 | + arr := strings.SplitN(strings.TrimPrefix(key, prefix), ":", 2) |
| 81 | + if len(arr) != 2 { |
| 82 | + log.ZError(ctx, "token value is not int", err, "key", key, "value", flagStr, "userID", userID) |
| 83 | + continue |
| 84 | + } |
| 85 | + platformID, err := strconv.Atoi(arr[0]) |
| 86 | + if err != nil { |
| 87 | + log.ZError(ctx, "token value is not int", err, "key", key, "value", flagStr, "userID", userID) |
| 88 | + continue |
| 89 | + } |
| 90 | + token := arr[1] |
| 91 | + if token == "" { |
| 92 | + log.ZError(ctx, "token value is not int", err, "key", key, "value", flagStr, "userID", userID) |
| 93 | + continue |
| 94 | + } |
| 95 | + tk, ok := res[platformID] |
| 96 | + if !ok { |
| 97 | + tk = make(map[string]int) |
| 98 | + res[platformID] = tk |
| 99 | + } |
| 100 | + tk[token] = flag |
| 101 | + } |
| 102 | + return res, nil |
| 103 | +} |
| 104 | + |
| 105 | +func (x *tokenCache) SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error { |
| 106 | + for token, flag := range m { |
| 107 | + err := x.SetTokenFlag(ctx, userID, platformID, token, flag) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + } |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (x *tokenCache) BatchSetTokenMapByUidPid(ctx context.Context, tokens map[string]map[string]any) error { |
| 116 | + for prefix, tokenFlag := range tokens { |
| 117 | + for token, flag := range tokenFlag { |
| 118 | + flagStr := fmt.Sprintf("%v", flag) |
| 119 | + if err := x.cache.Set(ctx, prefix+":"+token, flagStr, x.accessExpire); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func (x *tokenCache) DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error { |
| 128 | + keys := make([]string, 0, len(fields)) |
| 129 | + for _, token := range fields { |
| 130 | + keys = append(keys, x.getTokenKey(userID, platformID, token)) |
| 131 | + } |
| 132 | + return x.cache.Del(ctx, keys) |
| 133 | +} |
| 134 | + |
| 135 | +func (x *tokenCache) getExpireTime(t int64) time.Duration { |
| 136 | + return time.Hour * 24 * time.Duration(t) |
| 137 | +} |
| 138 | + |
| 139 | +func (x *tokenCache) DeleteTokenByTokenMap(ctx context.Context, userID string, tokens map[int][]string) error { |
| 140 | + keys := make([]string, 0, len(tokens)) |
| 141 | + for platformID, ts := range tokens { |
| 142 | + for _, t := range ts { |
| 143 | + keys = append(keys, x.getTokenKey(userID, platformID, t)) |
| 144 | + } |
| 145 | + } |
| 146 | + return x.cache.Del(ctx, keys) |
| 147 | +} |
| 148 | + |
| 149 | +func (x *tokenCache) DeleteAndSetTemporary(ctx context.Context, userID string, platformID int, fields []string) error { |
| 150 | + keys := make([]string, 0, len(fields)) |
| 151 | + for _, f := range fields { |
| 152 | + keys = append(keys, x.getTokenKey(userID, platformID, f)) |
| 153 | + } |
| 154 | + if err := x.cache.Del(ctx, keys); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + for _, f := range fields { |
| 159 | + k := cachekey.GetTemporaryTokenKey(userID, platformID, f) |
| 160 | + if err := x.cache.Set(ctx, k, "", time.Minute*5); err != nil { |
| 161 | + return errs.Wrap(err) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
0 commit comments