Skip to content

Commit b2936ce

Browse files
committed
Updated sample with new code
1 parent 2c589dd commit b2936ce

File tree

3 files changed

+174
-19
lines changed

3 files changed

+174
-19
lines changed

articles/redis/entra-for-authentication.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ If you have a cache where you use access keys, and you want to disable access ke
6666

6767
Because most Azure Managed Redis clients assume that a password and access key are used for authentication, you likely need to update your client workflow to support authentication by using Microsoft Entra. In this section, you learn how to configure your client applications to connect to Azure Managed Redis by using a Microsoft Entra token.
6868

69+
### Add users or System principal to your cache
70+
71+
1. Connect to your cache in the Azure portal
72+
73+
1. On the Resource menu, select **Authentication**.
74+
1. On the **Microsoft Entra Authentication** tab, select **User or service principal** and then **+ Select member**.
75+
1. Type the name of the user who you want to run the program. Select a user from the list and then **Select**. The user is added to the list of Redis users.
76+
1.
77+
:::image type="content" source="media/entra-for-authentication/redis-add-user.png" alt-text="Screenshot of the authentication tab in a Redis cache on the Azure portal.":::
78+
6979
### Microsoft Entra client workflow
7080

7181
1. Configure your client application to acquire a Microsoft Entra token for scope, `https://redis.azure.com/.default` or `acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default`, by using the [Microsoft Authentication Library (MSAL)](/azure/active-directory/develop/msal-overview).

articles/redis/go-get-started.md

Lines changed: 164 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Use Azure Cache for Redis with Go
33
description: In this quickstart, you learn how to create a Go app that uses Azure Cache for Redis.
4-
ms.date: 05/18/2025
4+
ms.date: 06/26/2025
55
ms.topic: quickstart
66
ms.custom:
77
- mode-api
@@ -16,47 +16,192 @@ zone_pivot_groups: redis-type
1616

1717
# Quickstart: Use Azure Redis with Go
1818

19-
In this article, you learn how to use a Azure Redis cache with the Go language.
20-
21-
<!-- ## Skip to the code on GitHub
22-
23-
If you want to skip straight to the code, see the [Go quickstart](https://github.com/Azure-Samples/azure-redis-cache-go-quickstart/) on GitHub.
24-
25-
We are breaking the connection to this. -->
19+
In this article, you learn how to use a Azure Redis cache with the Go language and connect using Microsoft Entra ID.
2620

2721
## Prerequisites
2822

2923
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
30-
- [Go](https://go.dev/doc/install) (preferably version 1.13 or above)
24+
- [Go](https://go.dev/doc/install)
25+
- You must also add two imports from Redis to your project and add them to your development environment.
26+
- `entraid "github.com/redis/go-redis-entraid"`
27+
- `"github.com/redis/go-redis/v9"`
3128

3229
## Create an Azure Managed Redis instance
3330

34-
First you must create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal.
31+
First you must create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal. In this Quickstart, we use Azure Managed Redis.
32+
33+
When you create the cache you should create it Microsoft Entra ID is enabled by default. Your cache must also use public endpoint for this QuickStart.
34+
35+
To create a chace with the portal, follow one of these procedure
3536

36-
When you create the cache you should create it with Access keys enabled. Microsoft Entra ID is enabled by default. Your cache must also use public endpoint for this Quickstart.
3737
- [Azure Managed Redis](includes/managed-redis-create.md)
3838
- [Azure Cache for Redis](/azure/azure-cache-for-redis/quickstart-create-redis)
3939

4040
Optionally, you can create a cache using Azure CLI, PowerShell, or any means that you prefer.
4141

42-
4342
## Code to connect to a AMR Cache
4443

45-
<!-- similar to python code. Use very basic defaultcredential with Redis extension-->
44+
The first part of the code sample, you set your connection to your cache:
45+
46+
```go
47+
package main
48+
49+
import (
50+
"context"
51+
"crypto/tls"
52+
"fmt"
53+
"log"
54+
"time"
55+
56+
entraid "github.com/redis/go-redis-entraid"
57+
"github.com/redis/go-redis/v9"
58+
)
59+
60+
func main() {
61+
ctx := context.Background()
62+
63+
// Set your Redis endpoint (hostname:port) from the cache you created.
64+
redisHost := "<host >:<public port number>"
65+
66+
// Create a credentials provider using DefaultAzureCredential
67+
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{})
68+
69+
if err != nil {
70+
log.Fatalf("Failed to create credentials provider: %v", err)
71+
}
72+
73+
// Create Redis client with Entra ID authentication
74+
client := redis.NewClient(&redis.Options{
75+
Addr: redisHost,
76+
TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12},
77+
WriteTimeout: 5 * time.Second,
78+
StreamingCredentialsProvider: provider,
79+
})
80+
defer client.Close()
81+
```
4682
4783
## Code to test a connection
4884
49-
<!-- similar to P -->
85+
In the next section, we test the connection using the Redis command "ping" that returns the "pong" string.
5086
51-
## Code set a key, get a key
87+
```go
88+
// Ping the Redis server to test the connection
89+
pong, err := client.Ping(ctx).Result()
90+
if err != nil {
91+
log.Fatal("Failed to connect to Redis:", err)
92+
}
93+
fmt.Println("Ping returned: ", pong)
94+
```
5295
53-
<!-- simple set key, get value -->
96+
## Code set a key, get a key
5497
55-
<!-- clean up resources include -->
98+
In this section, we show a basic `set` and `get` sequence to demonstrte using the Redis cache.
99+
100+
```go
101+
// Do something with Redis and a key-value pair
102+
result, err := client.Set(ctx, "Message", "Hello, The cache is working with Go!", 0).Result()
103+
if err != nil {
104+
log.Fatal("SET Message failed:", err)
105+
}
106+
fmt.Println("SET Message succeeded:", result)
107+
108+
value, err := client.Get(ctx, "Message").Result()
109+
if err != nil {
110+
if err == redis.Nil {
111+
fmt.Println("GET Message returned: key does not exist")
112+
} else {
113+
log.Fatal("GET Message failed:", err)
114+
}
115+
} else {
116+
fmt.Println("GET Message returned:", value)
117+
}
118+
119+
}
120+
121+
```
122+
123+
Before you can run this code, you must add yourself as a Redis user. And you must also authorize your connection to Azure from the command line using the Azure command line or Azure developer command line (azd).
124+
125+
You should also [add any user](entra-for-authentication.md) [Add users or System principal to your cache](entra-for-authentication.md#add-users-or-system-principal-to-your-cache) who might run the program as a user on the Redis cache.
126+
127+
The result looks like this:
128+
129+
```
130+
Ping returned: PONG
131+
SET Message succeeded: OK
132+
GET Message returned: Hello, The cache is working with Go!
133+
```
134+
You can see this code sample in its entirety.
135+
136+
```go
137+
package main
138+
139+
import (
140+
"context"
141+
"crypto/tls"
142+
"fmt"
143+
"log"
144+
"time"
145+
146+
entraid "github.com/redis/go-redis-entraid"
147+
"github.com/redis/go-redis/v9"
148+
)
149+
150+
func main() {
151+
ctx := context.Background()
152+
153+
// Set your Redis host (hostname:port)
154+
redisHost := "<host >:<public port number>""
155+
156+
// Create a credentials provider using DefaultAzureCredential
157+
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{})
158+
159+
if err != nil {
160+
log.Fatalf("Failed to create credentials provider: %v", err)
161+
}
162+
163+
// Create Redis client with Entra ID authentication
164+
client := redis.NewClient(&redis.Options{
165+
Addr: redisHost,
166+
TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12},
167+
WriteTimeout: 5 * time.Second,
168+
StreamingCredentialsProvider: provider,
169+
})
170+
defer client.Close()
171+
172+
// Ping the Redis server to test the connection
173+
pong, err := client.Ping(ctx).Result()
174+
if err != nil {
175+
log.Fatal("Failed to connect to Redis:", err)
176+
}
177+
fmt.Println("Ping returned: ", pong)
178+
179+
// Do something with Redis and a key-value pair
180+
result, err := client.Set(ctx, "Message", "Hello, The cache is working with Go!", 0).Result()
181+
if err != nil {
182+
log.Fatal("SET Message failed:", err)
183+
}
184+
fmt.Println("SET Message succeeded:", result)
185+
186+
value, err := client.Get(ctx, "Message").Result()
187+
if err != nil {
188+
if err == redis.Nil {
189+
fmt.Println("GET Message returned: key does not exist")
190+
} else {
191+
log.Fatal("GET Message failed:", err)
192+
}
193+
} else {
194+
fmt.Println("GET Message returned:", value)
195+
}
196+
197+
}
198+
```
199+
200+
<!-- Clean up resources include -->
56201
57202
[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)]
58203
59204
## Related content
60205
61-
<!-- Link to Redis Extension for connecting -->
62-
<!-- Link to any Redis code sample on their site that are germane -->
206+
- [Redis Extension for connecting using Microsoft Entra ID](https://github.com/redis/go-redis-entraid)
207+
- [go-Redis guide](https://redis.io/docs/latest/develop/clients/go/)
63.9 KB
Loading

0 commit comments

Comments
 (0)