You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/cache/user.go

123 lines
3.3 KiB

2 years ago
package cache
import (
"Open_IM/pkg/common/db/relation"
2 years ago
"Open_IM/pkg/common/db/table"
2 years ago
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
"encoding/json"
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
2 years ago
"strconv"
2 years ago
"time"
)
const (
2 years ago
userExpireTime = time.Second * 60 * 60 * 12
userInfoKey = "USER_INFO:"
userGlobalRecvMsgOptKey = "USER_GLOBAL_RECV_MSG_OPT_KEY:"
2 years ago
)
type UserCache struct {
2 years ago
userDB *relation.UserGorm
2 years ago
expireTime time.Duration
redisClient *RedisClient
rcClient *rockscache.Client
}
2 years ago
func NewUserCache(rdb redis.UniversalClient, userDB *relation.UserGorm, options rockscache.Options) *UserCache {
2 years ago
return &UserCache{
userDB: userDB,
2 years ago
expireTime: userExpireTime,
2 years ago
redisClient: NewRedisClient(rdb),
rcClient: rockscache.NewClient(rdb, options),
}
}
func (u *UserCache) getUserInfoKey(userID string) string {
return userInfoKey + userID
}
2 years ago
func (u *UserCache) getUserGlobalRecvMsgOptKey(userID string) string {
return userGlobalRecvMsgOptKey + userID
}
func (u *UserCache) GetUserInfo(ctx context.Context, userID string) (userInfo *table.UserModel, err error) {
2 years ago
getUserInfo := func() (string, error) {
userInfo, err := u.userDB.Take(ctx, userID)
if err != nil {
return "", err
}
bytes, err := json.Marshal(userInfo)
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
}
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "userInfo", *userInfo)
}()
2 years ago
userInfoStr, err := u.rcClient.Fetch(u.getUserInfoKey(userID), u.expireTime, getUserInfo)
2 years ago
if err != nil {
return nil, err
}
2 years ago
userInfo = &table.UserModel{}
2 years ago
err = json.Unmarshal([]byte(userInfoStr), userInfo)
return userInfo, utils.Wrap(err, "")
}
2 years ago
func (u *UserCache) GetUsersInfo(ctx context.Context, userIDs []string) ([]*table.UserModel, error) {
var users []*table.UserModel
2 years ago
for _, userID := range userIDs {
user, err := GetUserInfoFromCache(ctx, userID)
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}
func (u *UserCache) DelUserInfo(ctx context.Context, userID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
2 years ago
return u.rcClient.TagAsDeleted(u.getUserInfoKey(userID))
2 years ago
}
func (u *UserCache) DelUsersInfo(ctx context.Context, userIDs []string) (err error) {
for _, userID := range userIDs {
if err := u.DelUserInfo(ctx, userID); err != nil {
return err
}
}
return nil
}
2 years ago
func (u *UserCache) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
getUserGlobalRecvMsgOpt := func() (string, error) {
userInfo, err := u.userDB.Take(ctx, userID)
if err != nil {
return "", err
}
return strconv.Itoa(int(userInfo.GlobalRecvMsgOpt)), nil
}
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "opt", opt)
}()
optStr, err := u.rcClient.Fetch(u.getUserInfoKey(userID), u.expireTime, getUserGlobalRecvMsgOpt)
if err != nil {
return 0, err
}
return strconv.Atoi(optStr)
}
func (u *UserCache) DelUserGlobalRecvMsgOpt(ctx context.Context, userID string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
return u.rcClient.TagAsDeleted(u.getUserGlobalRecvMsgOptKey(userID))
}