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

110 lines
3.3 KiB

2 years ago
package cache
import (
"context"
2 years ago
"time"
2 years ago
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
2 years ago
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
)
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 {
2 years ago
metaCache
NewCache() UserCache
GetUserInfo(ctx context.Context, userID string) (userInfo *relationTb.UserModel, err error)
GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationTb.UserModel, error)
DelUsersInfo(userIDs []string) UserCache
GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error)
DelUsersGlobalRecvMsgOpt(userIDs []string) UserCache
2 years ago
}
type UserCacheRedis struct {
2 years ago
metaCache
userDB relationTb.UserModelInterface
2 years ago
expireTime time.Duration
2 years ago
rcClient *rockscache.Client
2 years ago
}
2 years ago
func NewUserCacheRedis(rdb redis.UniversalClient, userDB relationTb.UserModelInterface, options rockscache.Options) UserCache {
rcClient := rockscache.NewClient(rdb, options)
2 years ago
return &UserCacheRedis{
2 years ago
metaCache: NewMetaCacheRedis(rcClient),
2 years ago
userDB: userDB,
expireTime: userExpireTime,
2 years ago
rcClient: rcClient,
}
}
func (u *UserCacheRedis) NewCache() UserCache {
return &UserCacheRedis{
2 years ago
metaCache: NewMetaCacheRedis(u.rcClient, u.metaCache.GetPreDelKeys()...),
2 years ago
userDB: u.userDB,
expireTime: u.expireTime,
rcClient: u.rcClient,
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
return getCache(ctx, u.rcClient, u.getUserInfoKey(userID), u.expireTime, func(ctx context.Context) (*relationTb.UserModel, error) {
return u.userDB.Take(ctx, userID)
})
2 years ago
}
2 years ago
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationTb.UserModel, error) {
2 years ago
var keys []string
for _, userID := range userIDs {
keys = append(keys, u.getUserInfoKey(userID))
}
return batchGetCache(ctx, u.rcClient, keys, u.expireTime, func(user *relationTb.UserModel, keys []string) (int, error) {
for i, key := range keys {
if key == u.getUserInfoKey(user.UserID) {
return i, nil
}
}
return 0, errIndex
}, func(ctx context.Context) ([]*relationTb.UserModel, error) {
return u.userDB.Find(ctx, userIDs)
})
2 years ago
}
2 years ago
func (u *UserCacheRedis) DelUsersInfo(userIDs []string) UserCache {
var keys []string
2 years ago
for _, userID := range userIDs {
2 years ago
keys = append(keys, u.getUserInfoKey(userID))
2 years ago
}
2 years ago
cache := u.NewCache()
cache.AddKeys(keys...)
return cache
2 years ago
}
2 years ago
2 years ago
func (u *UserCacheRedis) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
2 years ago
return getCache(ctx, u.rcClient, u.getUserGlobalRecvMsgOptKey(userID), u.expireTime, func(ctx context.Context) (int, error) {
return u.userDB.GetUserGlobalRecvMsgOpt(ctx, userID)
})
2 years ago
}
2 years ago
func (u *UserCacheRedis) DelUsersGlobalRecvMsgOpt(userIDs []string) UserCache {
var keys []string
for _, userID := range userIDs {
keys = append(keys, u.getUserGlobalRecvMsgOptKey(userID))
}
cache := u.NewCache()
cache.AddKeys(keys...)
return cache
2 years ago
}