复制项目

This commit is contained in:
kim.dev.6789
2026-01-14 22:35:45 +08:00
parent 305d526110
commit b7f8db7d08
297 changed files with 81784 additions and 0 deletions

50
pkg/common/db/cache/imtoken.go vendored Normal file
View File

@@ -0,0 +1,50 @@
package cache
import (
"context"
"time"
"github.com/openimsdk/tools/errs"
"github.com/redis/go-redis/v9"
)
const (
chatPrefix = "CHAT:"
imToken = chatPrefix + "IM_TOKEN:"
)
func getIMTokenKey(userID string) string {
return imToken + userID
}
type IMTokenInterface interface {
GetToken(ctx context.Context, userID string) (string, error)
SetToken(ctx context.Context, userID, token string) error
}
type imTokenCacheRedis struct {
rdb redis.UniversalClient
expire time.Duration
}
func NewIMTokenInterface(rdb redis.UniversalClient, expire int) IMTokenInterface {
return &imTokenCacheRedis{rdb: rdb, expire: time.Duration(expire) * time.Minute}
}
func (i *imTokenCacheRedis) GetToken(ctx context.Context, userID string) (string, error) {
key := getIMTokenKey(userID)
token, err := i.rdb.Get(ctx, key).Result()
if err != nil {
return "", errs.Wrap(err)
}
return token, nil
}
func (i *imTokenCacheRedis) SetToken(ctx context.Context, userID, token string) error {
key := getIMTokenKey(userID)
err := i.rdb.Set(ctx, key, token, i.expire).Err()
if err != nil {
return errs.Wrap(err)
}
return nil
}

84
pkg/common/db/cache/token.go vendored Normal file
View File

@@ -0,0 +1,84 @@
// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cache
import (
"context"
"time"
"github.com/openimsdk/tools/utils/stringutil"
"github.com/openimsdk/tools/errs"
"github.com/redis/go-redis/v9"
)
const (
chatToken = "CHAT_UID_TOKEN_STATUS:"
)
type TokenInterface interface {
AddTokenFlag(ctx context.Context, userID string, token string, flag int) error
AddTokenFlagNXEx(ctx context.Context, userID string, token string, flag int, expire time.Duration) (bool, error)
GetTokensWithoutError(ctx context.Context, userID string) (map[string]int32, error)
DeleteTokenByUid(ctx context.Context, userID string) error
}
type TokenCacheRedis struct {
rdb redis.UniversalClient
accessExpire int64
}
func NewTokenInterface(rdb redis.UniversalClient) *TokenCacheRedis {
return &TokenCacheRedis{rdb: rdb}
}
func (t *TokenCacheRedis) AddTokenFlag(ctx context.Context, userID string, token string, flag int) error {
key := chatToken + userID
return errs.Wrap(t.rdb.HSet(ctx, key, token, flag).Err())
}
func (t *TokenCacheRedis) AddTokenFlagNXEx(ctx context.Context, userID string, token string, flag int, expire time.Duration) (bool, error) {
key := chatToken + userID
isSet, err := t.rdb.HSetNX(ctx, key, token, flag).Result()
if err != nil {
return false, errs.Wrap(err)
}
if !isSet {
// key already exists
return false, nil
}
if err = t.rdb.Expire(ctx, key, expire).Err(); err != nil {
return false, errs.Wrap(err)
}
return isSet, nil
}
func (t *TokenCacheRedis) GetTokensWithoutError(ctx context.Context, userID string) (map[string]int32, error) {
key := chatToken + userID
m, err := t.rdb.HGetAll(ctx, key).Result()
if err != nil {
return nil, errs.Wrap(err)
}
mm := make(map[string]int32)
for k, v := range m {
mm[k] = stringutil.StringToInt32(v)
}
return mm, nil
}
func (t *TokenCacheRedis) DeleteTokenByUid(ctx context.Context, userID string) error {
key := chatToken + userID
return errs.Wrap(t.rdb.Del(ctx, key).Err())
}

86
pkg/common/db/cache/user_login_ip.go vendored Normal file
View File

@@ -0,0 +1,86 @@
// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cache
import (
"context"
"time"
"github.com/openimsdk/tools/errs"
"github.com/redis/go-redis/v9"
)
const (
userLoginIPPrefix = "CHAT:USER_LOGIN_IP:"
// 缓存过期时间7天用户登录IP变化不频繁可以设置较长的过期时间
userLoginIPExpire = 7 * 24 * time.Hour
)
func getUserLoginIPKey(userID string) string {
return userLoginIPPrefix + userID
}
type UserLoginIPInterface interface {
// GetLatestLoginIP 获取用户最新登录IP从缓存
// 返回值:(ip, found, error)
// - ip: 缓存的IP值如果found为false则ip为空字符串
// - found: 是否在缓存中找到true表示缓存命中false表示缓存未命中
// - error: 错误信息
GetLatestLoginIP(ctx context.Context, userID string) (string, bool, error)
// SetLatestLoginIP 设置用户最新登录IP到缓存
SetLatestLoginIP(ctx context.Context, userID, ip string) error
// DeleteLatestLoginIP 删除用户最新登录IP缓存用于保证缓存一致性
DeleteLatestLoginIP(ctx context.Context, userID string) error
}
type userLoginIPCacheRedis struct {
rdb redis.UniversalClient
}
func NewUserLoginIPInterface(rdb redis.UniversalClient) UserLoginIPInterface {
return &userLoginIPCacheRedis{rdb: rdb}
}
func (u *userLoginIPCacheRedis) GetLatestLoginIP(ctx context.Context, userID string) (string, bool, error) {
key := getUserLoginIPKey(userID)
ip, err := u.rdb.Get(ctx, key).Result()
if err != nil {
if err == redis.Nil {
// 缓存不存在,返回 false 表示缓存未命中
return "", false, nil
}
return "", false, errs.Wrap(err)
}
// 缓存命中,返回 true
return ip, true, nil
}
func (u *userLoginIPCacheRedis) SetLatestLoginIP(ctx context.Context, userID, ip string) error {
key := getUserLoginIPKey(userID)
err := u.rdb.Set(ctx, key, ip, userLoginIPExpire).Err()
if err != nil {
return errs.Wrap(err)
}
return nil
}
func (u *userLoginIPCacheRedis) DeleteLatestLoginIP(ctx context.Context, userID string) error {
key := getUserLoginIPKey(userID)
err := u.rdb.Del(ctx, key).Err()
if err != nil {
return errs.Wrap(err)
}
return nil
}