The correct way to obtain the key list of all nodes in the Redis cluster? #2857
fenggolang
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
first method
go func GetRedisClusterPatternKeys(ctx context.Context, pattern string, clusterClient *redis.ClusterClient) ([]string, error) { var mu sync.Mutex allKey := make([]string, 0) err := clusterClient.ForEachSlave(ctx, func(ctx context.Context, client *redis.Client) error { mu.Lock() defer mu.Unlock() keys, err := client.Keys(ctx, pattern).Result() if err != nil { logrus.Errorf("get redis slave keys(%v) failed:%v", pattern, err.Error()) return nil } allKey = append(allKey, keys...) return nil }) if err != nil { return nil, err } return allKey, nil }
second method:
go func GetRedisClusterPatternKeysNoLock(ctx context.Context, pattern string, clusterClient *redis.ClusterClient) ([]string, error) { defer func() { if err := recover(); err != nil { logrus.Errorf("%v", err) } }() allKey := make([]string, 0) err := clusterClient.ForEachSlave(ctx, func(ctx context.Context, client *redis.Client) error { keys, err := client.Keys(ctx, pattern).Result() if err != nil { logrus.Errorf("get redis slave keys(%v) failed:%v", pattern, err.Error()) return nil } allKey = append(allKey, keys...) return nil }) if err != nil { return nil, err } return allKey, nil }
Beta Was this translation helpful? Give feedback.
All reactions