GetMultiKey gets multiple keys from the database.
(keys []string)
| 407 | |
| 408 | // GetMultiKey gets multiple keys from the database. |
| 409 | func (r *RedisCluster) GetMultiKey(keys []string) ([]string, error) { |
| 410 | if err := r.up(); err != nil { |
| 411 | return nil, err |
| 412 | } |
| 413 | cluster := r.singleton() |
| 414 | keyNames := make([]string, len(keys)) |
| 415 | copy(keyNames, keys) |
| 416 | for index, val := range keyNames { |
| 417 | keyNames[index] = r.fixKey(val) |
| 418 | } |
| 419 | |
| 420 | result := make([]string, 0) |
| 421 | |
| 422 | switch v := cluster.(type) { |
| 423 | case *redis.ClusterClient: |
| 424 | { |
| 425 | getCmds := make([]*redis.StringCmd, 0) |
| 426 | pipe := v.Pipeline() |
| 427 | for _, key := range keyNames { |
| 428 | getCmds = append(getCmds, pipe.Get(key)) |
| 429 | } |
| 430 | _, err := pipe.Exec() |
| 431 | if err != nil && !errors.Is(err, redis.Nil) { |
| 432 | log.Debugf("Error trying to get value: %s", err.Error()) |
| 433 | |
| 434 | return nil, ErrKeyNotFound |
| 435 | } |
| 436 | for _, cmd := range getCmds { |
| 437 | result = append(result, cmd.Val()) |
| 438 | } |
| 439 | } |
| 440 | case *redis.Client: |
| 441 | { |
| 442 | values, err := cluster.MGet(keyNames...).Result() |
| 443 | if err != nil { |
| 444 | log.Debugf("Error trying to get value: %s", err.Error()) |
| 445 | |
| 446 | return nil, ErrKeyNotFound |
| 447 | } |
| 448 | for _, val := range values { |
| 449 | strVal := fmt.Sprint(val) |
| 450 | if strVal == "<nil>" { |
| 451 | strVal = "" |
| 452 | } |
| 453 | result = append(result, strVal) |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | for _, val := range result { |
| 459 | if val != "" { |
| 460 | return result, nil |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | return nil, ErrKeyNotFound |
| 465 | } |
| 466 |