Files
open-im-server/pkg/common/db/cache/user.go
T

110 lines
3.3 KiB
Go
Raw Normal View History

2023-01-31 18:55:22 +08:00
package cache
import (
"context"
2023-03-23 19:02:20 +08:00
"time"
2023-03-16 10:46:06 +08:00
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
2023-01-31 18:55:22 +08:00
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
)
const (
2023-02-02 19:40:54 +08:00
userExpireTime = time.Second * 60 * 60 * 12
userInfoKey = "USER_INFO:"
userGlobalRecvMsgOptKey = "USER_GLOBAL_RECV_MSG_OPT_KEY:"
2023-01-31 18:55:22 +08:00
)
2023-02-09 20:36:34 +08:00
type UserCache interface {
2023-03-23 19:02:20 +08:00
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
2023-02-09 20:36:34 +08:00
}
type UserCacheRedis struct {
2023-03-23 19:02:20 +08:00
metaCache
userDB relationTb.UserModelInterface
2023-03-01 15:32:26 +08:00
expireTime time.Duration
2023-03-23 19:02:20 +08:00
rcClient *rockscache.Client
2023-01-31 18:55:22 +08:00
}
2023-03-23 19:02:20 +08:00
func NewUserCacheRedis(rdb redis.UniversalClient, userDB relationTb.UserModelInterface, options rockscache.Options) UserCache {
rcClient := rockscache.NewClient(rdb, options)
2023-02-09 20:36:34 +08:00
return &UserCacheRedis{
2023-03-23 19:02:20 +08:00
metaCache: NewMetaCacheRedis(rcClient),
2023-03-01 15:32:26 +08:00
userDB: userDB,
expireTime: userExpireTime,
2023-03-23 19:02:20 +08:00
rcClient: rcClient,
}
}
func (u *UserCacheRedis) NewCache() UserCache {
return &UserCacheRedis{
2023-03-24 18:53:49 +08:00
metaCache: NewMetaCacheRedis(u.rcClient, u.metaCache.GetPreDeleteKeys()...),
2023-03-23 19:02:20 +08:00
userDB: u.userDB,
expireTime: u.expireTime,
rcClient: u.rcClient,
2023-01-31 18:55:22 +08:00
}
}
2023-02-09 20:36:34 +08:00
func (u *UserCacheRedis) getUserInfoKey(userID string) string {
2023-01-31 18:55:22 +08:00
return userInfoKey + userID
}
2023-02-09 20:36:34 +08:00
func (u *UserCacheRedis) getUserGlobalRecvMsgOptKey(userID string) string {
2023-02-02 19:40:54 +08:00
return userGlobalRecvMsgOptKey + userID
}
2023-02-09 20:36:34 +08:00
func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userInfo *relationTb.UserModel, err error) {
2023-03-23 19:02:20 +08:00
return getCache(ctx, u.rcClient, u.getUserInfoKey(userID), u.expireTime, func(ctx context.Context) (*relationTb.UserModel, error) {
return u.userDB.Take(ctx, userID)
})
2023-01-31 18:55:22 +08:00
}
2023-02-09 20:36:34 +08:00
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationTb.UserModel, error) {
2023-03-23 19:02:20 +08:00
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)
})
2023-01-31 18:55:22 +08:00
}
2023-03-23 19:02:20 +08:00
func (u *UserCacheRedis) DelUsersInfo(userIDs []string) UserCache {
var keys []string
2023-01-31 18:55:22 +08:00
for _, userID := range userIDs {
2023-03-23 19:02:20 +08:00
keys = append(keys, u.getUserInfoKey(userID))
2023-01-31 18:55:22 +08:00
}
2023-03-23 19:02:20 +08:00
cache := u.NewCache()
cache.AddKeys(keys...)
return cache
2023-01-31 18:55:22 +08:00
}
2023-02-02 19:40:54 +08:00
2023-02-09 20:36:34 +08:00
func (u *UserCacheRedis) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
2023-03-23 19:02:20 +08:00
return getCache(ctx, u.rcClient, u.getUserGlobalRecvMsgOptKey(userID), u.expireTime, func(ctx context.Context) (int, error) {
return u.userDB.GetUserGlobalRecvMsgOpt(ctx, userID)
})
2023-02-02 19:40:54 +08:00
}
2023-03-23 19:02:20 +08:00
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
2023-02-02 19:40:54 +08:00
}