| 8 | ) |
| 9 | |
| 10 | func main() { |
| 11 | ctx := context.Background() |
| 12 | |
| 13 | // Create a cluster client |
| 14 | rdb := redis.NewClusterClient(&redis.ClusterOptions{ |
| 15 | Addrs: []string{ |
| 16 | "localhost:16600", |
| 17 | "localhost:16601", |
| 18 | "localhost:16602", |
| 19 | "localhost:16603", |
| 20 | "localhost:16604", |
| 21 | "localhost:16605", |
| 22 | }, |
| 23 | }) |
| 24 | defer rdb.Close() |
| 25 | |
| 26 | // Test connection |
| 27 | if err := rdb.Ping(ctx).Err(); err != nil { |
| 28 | panic(fmt.Sprintf("Failed to connect to Redis cluster: %v", err)) |
| 29 | } |
| 30 | |
| 31 | fmt.Println("✓ Connected to Redis cluster") |
| 32 | |
| 33 | // Define 10 keys and values |
| 34 | keys := make([]string, 10) |
| 35 | values := make([]string, 10) |
| 36 | for i := 0; i < 10; i++ { |
| 37 | keys[i] = fmt.Sprintf("key%d", i) |
| 38 | values[i] = fmt.Sprintf("value%d", i) |
| 39 | } |
| 40 | |
| 41 | // Set all 10 keys |
| 42 | fmt.Println("\n=== Setting 10 keys ===") |
| 43 | for i := 0; i < 10; i++ { |
| 44 | err := rdb.Set(ctx, keys[i], values[i], 0).Err() |
| 45 | if err != nil { |
| 46 | panic(fmt.Sprintf("Failed to set %s: %v", keys[i], err)) |
| 47 | } |
| 48 | fmt.Printf("✓ SET %s = %s\n", keys[i], values[i]) |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | // Retrieve all keys using MGET |
| 53 | fmt.Println("\n=== Retrieving keys with MGET ===") |
| 54 | result, err := rdb.MGet(ctx, keys...).Result() |
| 55 | if err != nil { |
| 56 | panic(fmt.Sprintf("Failed to execute MGET: %v", err)) |
| 57 | } |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | // Validate the results |
| 62 | fmt.Println("\n=== Validating MGET results ===") |
| 63 | allValid := true |
| 64 | for i, val := range result { |
| 65 | expectedValue := values[i] |
| 66 | actualValue, ok := val.(string) |
| 67 | |