diff --git a/pkg/common/db/cache/meta_cache.go b/pkg/common/db/cache/meta_cache.go index ca742d4a3..71166bf33 100644 --- a/pkg/common/db/cache/meta_cache.go +++ b/pkg/common/db/cache/meta_cache.go @@ -106,13 +106,7 @@ func GetDefaultOpt() rockscache.Options { return opts } -func getCache[T any]( - ctx context.Context, - rcClient *rockscache.Client, - key string, - expire time.Duration, - fn func(ctx context.Context) (T, error), -) (T, error) { +func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) { var t T var write bool v, err := rcClient.Fetch2(ctx, key, expire, func() (s string, err error) { @@ -144,14 +138,7 @@ func getCache[T any]( return t, nil } -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) { +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) { batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) { values := make(map[int]string) tArrays, err := fn(ctx) diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index b821b4a52..74e93cfeb 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -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 } - 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) - }, - ) + 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 + } + res = append(res, val) + } + return res, nil +} + +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 {