|
|
|
@ -113,28 +113,50 @@ func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userIn
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) {
|
|
|
|
|
var keys []string
|
|
|
|
|
for _, userID := range userIDs {
|
|
|
|
|
keys = append(keys, u.getUserInfoKey(userID))
|
|
|
|
|
func batchGetCache2[T any, K comparable](ctx context.Context, rcClient *rockscache.Client, expire time.Duration, keys []K, keyFn func(key K) string, fns func(ctx context.Context, key K) (T, error)) ([]T, error) {
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
res := make([]T, 0, len(keys))
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
val, err := getCache(ctx, rcClient, keyFn(key), expire, func(ctx context.Context) (T, error) {
|
|
|
|
|
return fns(ctx, key)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
res = append(res, val)
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
return 0, errIndex
|
|
|
|
|
},
|
|
|
|
|
func(ctx context.Context) ([]*relationtb.UserModel, error) {
|
|
|
|
|
return u.userDB.Find(ctx, userIDs)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) {
|
|
|
|
|
//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)
|
|
|
|
|
// },
|
|
|
|
|
//)
|
|
|
|
|
return batchGetCache2(ctx, u.rcClient, u.expireTime, userIDs, func(userID string) string {
|
|
|
|
|
return u.getUserInfoKey(userID)
|
|
|
|
|
}, func(ctx context.Context, userID string) (*relationtb.UserModel, error) {
|
|
|
|
|
return u.userDB.Take(ctx, userID)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) UserCache {
|
|
|
|
|