Skip to content

Commit

Permalink
fix: redis get nil
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Mar 31, 2023
1 parent a0c635b commit dc26bb7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
2 changes: 1 addition & 1 deletion center/router/router_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (rt *Router) targetGets(c *gin.Context) {

if len(keys) > 0 {
metaMap := make(map[string]*models.HostMeta)
vals, _ := storage.MGet(context.Background(), rt.Redis, keys)
vals := storage.MGet(context.Background(), rt.Redis, keys)
for _, value := range vals {
var meta models.HostMeta
if value == nil {
Expand Down
12 changes: 2 additions & 10 deletions memsto/target_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ func (tc *TargetCacheType) GetHostMetas(targets []*models.Target) map[string]*mo
keys = append(keys, models.WrapIdent(targets[i].Ident))
num++
if num == 100 {
vals, err := storage.MGet(context.Background(), tc.redis, keys)
if err != nil {
logger.Warningf("keys:%v get host meta err:%v", keys, err)
continue
}

vals := storage.MGet(context.Background(), tc.redis, keys)
for _, value := range vals {
var meta models.HostMeta
if value == nil {
Expand All @@ -202,10 +197,7 @@ func (tc *TargetCacheType) GetHostMetas(targets []*models.Target) map[string]*mo
}
}

vals, err := storage.MGet(context.Background(), tc.redis, keys)
if err != nil {
logger.Warningf("keys:%v get host meta err:%v", keys, err)
}
vals := storage.MGet(context.Background(), tc.redis, keys)
for _, value := range vals {
var meta models.HostMeta
if value == nil {
Expand Down
10 changes: 3 additions & 7 deletions storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,13 @@ func NewRedis(cfg RedisConfig) (Redis, error) {
return redisClient, nil
}

func MGet(ctx context.Context, r Redis, keys []string) ([][]byte, error) {
func MGet(ctx context.Context, r Redis, keys []string) [][]byte {
var vals [][]byte
pipe := r.Pipeline()
for _, key := range keys {
pipe.Get(ctx, key)
}
cmds, err := pipe.Exec(ctx)
if err != nil {
logger.Errorf("failed to exec pipeline: %s", err)
return vals, err
}
cmds, _ := pipe.Exec(ctx)

for i, key := range keys {
cmd := cmds[i]
Expand All @@ -135,7 +131,7 @@ func MGet(ctx context.Context, r Redis, keys []string) ([][]byte, error) {
vals = append(vals, val)
}

return vals, err
return vals
}

func MSet(ctx context.Context, r Redis, m map[string]interface{}) error {
Expand Down

0 comments on commit dc26bb7

Please sign in to comment.