fix: redis get nil

This commit is contained in:
ning 2023-03-31 10:17:46 +08:00
parent a0c635b830
commit dc26bb78d8
3 changed files with 6 additions and 18 deletions

View File

@ -65,7 +65,7 @@ func (rt *Router) targetGets(c *gin.Context) {
if len(keys) > 0 { if len(keys) > 0 {
metaMap := make(map[string]*models.HostMeta) 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 { for _, value := range vals {
var meta models.HostMeta var meta models.HostMeta
if value == nil { if value == nil {

View File

@ -177,12 +177,7 @@ func (tc *TargetCacheType) GetHostMetas(targets []*models.Target) map[string]*mo
keys = append(keys, models.WrapIdent(targets[i].Ident)) keys = append(keys, models.WrapIdent(targets[i].Ident))
num++ num++
if num == 100 { if num == 100 {
vals, err := storage.MGet(context.Background(), tc.redis, keys) vals := storage.MGet(context.Background(), tc.redis, keys)
if err != nil {
logger.Warningf("keys:%v get host meta err:%v", keys, err)
continue
}
for _, value := range vals { for _, value := range vals {
var meta models.HostMeta var meta models.HostMeta
if value == nil { if value == nil {
@ -202,10 +197,7 @@ func (tc *TargetCacheType) GetHostMetas(targets []*models.Target) map[string]*mo
} }
} }
vals, err := storage.MGet(context.Background(), tc.redis, keys) vals := storage.MGet(context.Background(), tc.redis, keys)
if err != nil {
logger.Warningf("keys:%v get host meta err:%v", keys, err)
}
for _, value := range vals { for _, value := range vals {
var meta models.HostMeta var meta models.HostMeta
if value == nil { if value == nil {

View File

@ -113,17 +113,13 @@ func NewRedis(cfg RedisConfig) (Redis, error) {
return redisClient, nil 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 var vals [][]byte
pipe := r.Pipeline() pipe := r.Pipeline()
for _, key := range keys { for _, key := range keys {
pipe.Get(ctx, key) pipe.Get(ctx, key)
} }
cmds, err := pipe.Exec(ctx) cmds, _ := pipe.Exec(ctx)
if err != nil {
logger.Errorf("failed to exec pipeline: %s", err)
return vals, err
}
for i, key := range keys { for i, key := range keys {
cmd := cmds[i] cmd := cmds[i]
@ -135,7 +131,7 @@ func MGet(ctx context.Context, r Redis, keys []string) ([][]byte, error) {
vals = append(vals, val) vals = append(vals, val)
} }
return vals, err return vals
} }
func MSet(ctx context.Context, r Redis, m map[string]interface{}) error { func MSet(ctx context.Context, r Redis, m map[string]interface{}) error {