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

112 lines
3.0 KiB

2 years ago
package cache
import (
"context"
"encoding/json"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"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
)
2 years ago
type UserCache interface {
}
type UserCacheRedis struct {
2 years ago
userDB *relation.UserGorm
2 years ago
2 years ago
expireTime time.Duration
rcClient *rockscache.Client
2 years ago
}
2 years ago
func NewUserCacheRedis(rdb redis.UniversalClient, userDB *relation.UserGorm, options rockscache.Options) *UserCacheRedis {
return &UserCacheRedis{
2 years ago
userDB: userDB,
expireTime: userExpireTime,
rcClient: rockscache.NewClient(rdb, options),
2 years ago
}
}
2 years ago
func (u *UserCacheRedis) getUserInfoKey(userID string) string {
2 years ago
return userInfoKey + userID
}
2 years ago
func (u *UserCacheRedis) getUserGlobalRecvMsgOptKey(userID string) string {
2 years ago
return userGlobalRecvMsgOptKey + userID
}
2 years ago
func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userInfo *relationTb.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
}
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 = &relationTb.UserModel{}
2 years ago
err = json.Unmarshal([]byte(userInfoStr), userInfo)
return userInfo, utils.Wrap(err, "")
}
2 years ago
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationTb.UserModel, error) {
2 years ago
var users []*relationTb.UserModel
2 years ago
//for _, userID := range userIDs {
// user, err := GetUserInfoFromCache(ctx, userID)
// if err != nil {
// return nil, err
// }
// users = append(users, user)
//}
2 years ago
return users, nil
}
2 years ago
func (u *UserCacheRedis) DelUserInfo(ctx context.Context, userID string) (err error) {
2 years ago
return u.rcClient.TagAsDeleted(u.getUserInfoKey(userID))
2 years ago
}
2 years ago
func (u *UserCacheRedis) DelUsersInfo(ctx context.Context, userIDs []string) (err error) {
2 years ago
for _, userID := range userIDs {
if err := u.DelUserInfo(ctx, userID); err != nil {
return err
}
}
return nil
}
2 years ago
2 years ago
func (u *UserCacheRedis) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
2 years ago
getUserGlobalRecvMsgOpt := func() (string, error) {
userInfo, err := u.userDB.Take(ctx, userID)
if err != nil {
return "", err
}
return strconv.Itoa(int(userInfo.GlobalRecvMsgOpt)), nil
}
optStr, err := u.rcClient.Fetch(u.getUserInfoKey(userID), u.expireTime, getUserGlobalRecvMsgOpt)
if err != nil {
return 0, err
}
return strconv.Atoi(optStr)
}
2 years ago
func (u *UserCacheRedis) DelUserGlobalRecvMsgOpt(ctx context.Context, userID string) (err error) {
2 years ago
return u.rcClient.TagAsDeleted(u.getUserGlobalRecvMsgOptKey(userID))
}