|
| 1 | +# Redis Cluster MGET Example |
| 2 | + |
| 3 | +This example demonstrates how to use the Redis Cluster client with the `MGET` command to retrieve multiple keys efficiently. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The example shows: |
| 8 | +- Creating a Redis Cluster client |
| 9 | +- Setting 10 keys with individual `SET` commands |
| 10 | +- Retrieving all 10 keys in a single operation using `MGET` |
| 11 | +- Validating that the retrieved values match the expected values |
| 12 | +- Cleaning up by deleting the test keys |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +You need a running Redis Cluster. The example expects cluster nodes at: |
| 17 | +- `localhost:7000` |
| 18 | +- `localhost:7001` |
| 19 | +- `localhost:7002` |
| 20 | + |
| 21 | +### Setting up a Redis Cluster (using Docker) |
| 22 | + |
| 23 | +If you don't have a Redis Cluster running, you can use the docker-compose setup from the repository root: |
| 24 | + |
| 25 | +```bash |
| 26 | +# From the go-redis repository root |
| 27 | +docker compose --profile cluster up -d |
| 28 | +``` |
| 29 | + |
| 30 | +This will start a Redis Cluster with nodes on ports 16600-16605. |
| 31 | + |
| 32 | +If using the docker-compose cluster, update the `Addrs` in `main.go` to: |
| 33 | +```go |
| 34 | +Addrs: []string{ |
| 35 | + "localhost:16600", |
| 36 | + "localhost:16601", |
| 37 | + "localhost:16602", |
| 38 | +}, |
| 39 | +``` |
| 40 | + |
| 41 | +## Running the Example |
| 42 | + |
| 43 | +```bash |
| 44 | +go run main.go |
| 45 | +``` |
| 46 | + |
| 47 | +## Expected Output |
| 48 | + |
| 49 | +``` |
| 50 | +✓ Connected to Redis cluster |
| 51 | +
|
| 52 | +=== Setting 10 keys === |
| 53 | +✓ SET key0 = value0 |
| 54 | +✓ SET key1 = value1 |
| 55 | +✓ SET key2 = value2 |
| 56 | +✓ SET key3 = value3 |
| 57 | +✓ SET key4 = value4 |
| 58 | +✓ SET key5 = value5 |
| 59 | +✓ SET key6 = value6 |
| 60 | +✓ SET key7 = value7 |
| 61 | +✓ SET key8 = value8 |
| 62 | +✓ SET key9 = value9 |
| 63 | +
|
| 64 | +=== Retrieving keys with MGET === |
| 65 | +
|
| 66 | +=== Validating MGET results === |
| 67 | +✓ key0: value0 |
| 68 | +✓ key1: value1 |
| 69 | +✓ key2: value2 |
| 70 | +✓ key3: value3 |
| 71 | +✓ key4: value4 |
| 72 | +✓ key5: value5 |
| 73 | +✓ key6: value6 |
| 74 | +✓ key7: value7 |
| 75 | +✓ key8: value8 |
| 76 | +✓ key9: value9 |
| 77 | +
|
| 78 | +=== Summary === |
| 79 | +✓ All values retrieved successfully and match expected values! |
| 80 | +
|
| 81 | +=== Cleaning up === |
| 82 | +✓ Cleanup complete |
| 83 | +``` |
| 84 | + |
| 85 | +## Key Concepts |
| 86 | + |
| 87 | +### MGET Command |
| 88 | + |
| 89 | +`MGET` (Multiple GET) is a Redis command that retrieves the values of multiple keys in a single operation. This is more efficient than executing multiple individual `GET` commands. |
| 90 | + |
| 91 | +**Syntax:** |
| 92 | +```go |
| 93 | +result, err := rdb.MGet(ctx, key1, key2, key3, ...).Result() |
| 94 | +``` |
| 95 | + |
| 96 | +**Returns:** |
| 97 | +- A slice of `interface{}` values |
| 98 | +- Each value corresponds to a key in the same order |
| 99 | +- `nil` is returned for keys that don't exist |
| 100 | + |
| 101 | +### Cluster Client |
| 102 | + |
| 103 | +The `ClusterClient` automatically handles: |
| 104 | +- Distributing keys across cluster nodes based on hash slots |
| 105 | +- Following cluster redirects |
| 106 | +- Maintaining connections to all cluster nodes |
| 107 | +- Retrying operations on cluster topology changes |
| 108 | + |
| 109 | +For `MGET` operations in a cluster, the client may need to split the request across multiple nodes if the keys map to different hash slots. |
| 110 | + |
| 111 | +## Code Highlights |
| 112 | + |
| 113 | +```go |
| 114 | +// Create cluster client |
| 115 | +rdb := redis.NewClusterClient(&redis.ClusterOptions{ |
| 116 | + Addrs: []string{ |
| 117 | + "localhost:7000", |
| 118 | + "localhost:7001", |
| 119 | + "localhost:7002", |
| 120 | + }, |
| 121 | +}) |
| 122 | + |
| 123 | +// Set individual keys |
| 124 | +for i := 0; i < 10; i++ { |
| 125 | + err := rdb.Set(ctx, fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), 0).Err() |
| 126 | + // handle error |
| 127 | +} |
| 128 | + |
| 129 | +// Retrieve all keys with MGET |
| 130 | +result, err := rdb.MGet(ctx, keys...).Result() |
| 131 | + |
| 132 | +// Validate results |
| 133 | +for i, val := range result { |
| 134 | + actualValue, ok := val.(string) |
| 135 | + // validate actualValue matches expected |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Learn More |
| 140 | + |
| 141 | +- [Redis MGET Documentation](https://redis.io/commands/mget/) |
| 142 | +- [Redis Cluster Specification](https://redis.io/topics/cluster-spec) |
| 143 | +- [go-redis Documentation](https://redis.uptrace.dev/) |
| 144 | + |
0 commit comments