fix: GetUsersInfo cache

pull/1258/head
withchao 2 years ago
parent 8431faad8a
commit 89a98fcfca

@ -106,13 +106,7 @@ func GetDefaultOpt() rockscache.Options {
return opts return opts
} }
func getCache[T any]( func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) {
ctx context.Context,
rcClient *rockscache.Client,
key string,
expire time.Duration,
fn func(ctx context.Context) (T, error),
) (T, error) {
var t T var t T
var write bool var write bool
v, err := rcClient.Fetch2(ctx, key, expire, func() (s string, err error) { v, err := rcClient.Fetch2(ctx, key, expire, func() (s string, err error) {
@ -144,14 +138,7 @@ func getCache[T any](
return t, nil return t, nil
} }
func batchGetCache[T any]( func batchGetCache[T any](ctx context.Context, rcClient *rockscache.Client, keys []string, expire time.Duration, keyIndexFn func(t T, keys []string) (int, error), fn func(ctx context.Context) ([]T, error)) ([]T, error) {
ctx context.Context,
rcClient *rockscache.Client,
keys []string,
expire time.Duration,
keyIndexFn func(t T, keys []string) (int, error),
fn func(ctx context.Context) ([]T, error),
) ([]T, error) {
batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) {
values := make(map[int]string) values := make(map[int]string)
tArrays, err := fn(ctx) tArrays, err := fn(ctx)

@ -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) { 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) {
var keys []string if len(keys) == 0 {
for _, userID := range userIDs { return nil, nil
keys = append(keys, u.getUserInfoKey(userID))
} }
return batchGetCache( res := make([]T, 0, len(keys))
ctx, for _, key := range keys {
u.rcClient, val, err := getCache(ctx, rcClient, keyFn(key), expire, func(ctx context.Context) (T, error) {
keys, return fns(ctx, key)
u.expireTime, })
func(user *relationtb.UserModel, keys []string) (int, error) { if err != nil {
for i, key := range keys { return nil, err
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) { func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) {
return u.userDB.Find(ctx, userIDs) //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 { func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) UserCache {

Loading…
Cancel
Save