|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/redis/go-redis/v9" |
| 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 | +
|
| 68 | + if !ok { |
| 69 | + fmt.Printf("✗ %s: expected string, got %T\n", keys[i], val) |
| 70 | + allValid = false |
| 71 | + continue |
| 72 | + } |
| 73 | +
|
| 74 | + if actualValue != expectedValue { |
| 75 | + fmt.Printf("✗ %s: expected '%s', got '%s'\n", keys[i], expectedValue, actualValue) |
| 76 | + allValid = false |
| 77 | + } else { |
| 78 | + fmt.Printf("✓ %s: %s\n", keys[i], actualValue) |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + // Print summary |
| 83 | + fmt.Println("\n=== Summary ===") |
| 84 | + if allValid { |
| 85 | + fmt.Println("✓ All values retrieved successfully and match expected values!") |
| 86 | + } else { |
| 87 | + fmt.Println("✗ Some values did not match expected values") |
| 88 | + } |
| 89 | + */ |
| 90 | + |
| 91 | + // Clean up - delete the keys |
| 92 | + fmt.Println("\n=== Cleaning up ===") |
| 93 | + for _, key := range keys { |
| 94 | + if err := rdb.Del(ctx, key).Err(); err != nil { |
| 95 | + fmt.Printf("Warning: Failed to delete %s: %v\n", key, err) |
| 96 | + } |
| 97 | + } |
| 98 | + fmt.Println("✓ Cleanup complete") |
| 99 | + |
| 100 | + err := rdb.Set(ctx, "{tag}exists", "asdf",0).Err() |
| 101 | + if err != nil { |
| 102 | + panic(err) |
| 103 | + } |
| 104 | + val, err := rdb.Get(ctx, "{tag}nilkeykey1").Result() |
| 105 | + fmt.Printf("\nval: %+v err: %+v\n", val, err) |
| 106 | + valm, err := rdb.MGet(ctx, "{tag}nilkeykey1", "{tag}exists").Result() |
| 107 | + fmt.Printf("\nval: %+v err: %+v\n", valm, err) |
| 108 | +} |
0 commit comments