You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this article, you learn how to build a REST API in Go that stores and retrieves user information backed by a [HASH](https://redis.io/topics/data-types-intro#redis-hashes) data structure in [Azure Cache for Redis](overview.md).
19
+
In this article, you learn how to use a Azure Redis cache with the Go language.
19
20
20
-
## Skip to the code on GitHub
21
+
<!--## Skip to the code on GitHub
21
22
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.
23
24
25
+
We are breaking the connection to this. -->
26
+
24
27
## Prerequisites
25
28
26
29
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
27
30
-[Go](https://go.dev/doc/install) (preferably version 1.13 or above)
If you're interested in learning how the code works, you can review the following snippets. Otherwise, feel free to skip ahead to [Run the application](#run-the-application).
40
-
41
-
The open source [go-redis](https://github.com/go-redis/redis) library is used to interact with Azure Cache for Redis.
42
-
43
-
The `main` function starts off by reading the host name and password (Access Key) for the Azure Cache for Redis instance.
44
-
45
-
```go
46
-
funcmain() {
47
-
redisHost:= os.Getenv("REDIS_HOST")
48
-
redisPassword:= os.Getenv("REDIS_PASSWORD")
49
-
...
50
-
```
51
-
52
-
Then, we establish connection with Azure Cache for Redis. We use [tls.Config](https://go.dev/pkg/crypto/tls/#Config)--Azure Cache for Redis only accepts secure connections with [TLS 1.2 as the minimum required version]/azure-cache-for-redis/cache-remove-tls-10-11.md).
log.Fatalf("failed to connect with redis instance at %s - %v", redisHost, err)
63
-
}
64
-
...
65
-
```
66
-
67
-
If the connection is successful, [HTTP handlers](https://go.dev/pkg/net/http/#HandleFunc) are configured to handle `POST` and `GET` operations and the HTTP server is started.
68
-
69
-
> [!NOTE]
70
-
> [gorilla mux library](https://github.com/gorilla/mux) is used for routing (although it's not strictly necessary and we could have gotten away by using the standard library for this sample application).
First you must create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal.
79
37
80
-
log.Fatal(http.ListenAndServe(":8080", router))
81
-
```
38
+
When you create the cache you should create it with both Access keys enabled. Microsoft Entra ID is enabled by default. Your cache must also public endpoint for this Quickstart.
-[Azure Cache for Redis](/azure/azure-cache-for-redis/quickstart-create-redis)
82
41
83
-
`userHandler` struct encapsulates a [redis.Client](https://pkg.go.dev/github.com/go-redis/redis/v8#Client), which is used by the `createUser`, `getUser` methods - code for these methods isn't included for brevity.
42
+
Optionally, you can create a cache using Azure CLI, PowerShell, or any means that you prefer.
84
43
85
-
- `createUser`: accepts a JSON payload (containing user information) and saves it as a `HASH` in Azure Cache for Redis.
86
-
- `getUser`: fetches user info from `HASH` or returns an HTTP `404` response if not found.
The application accepts connectivity and credentials in the form of environment variables.
129
61
130
-
1. Fetch the **Host name** and **Access Keys** (available via Access Keys) for Azure Cache for Redis instance in the [Azure portal](https://portal.azure.com/)
131
62
132
-
1. Set them to the respective environment variables:
133
63
134
-
```console
135
-
set REDIS_HOST=<Host name>:<port> (e.g. <name of cache>.redis.cache.windows.net:6380)
136
-
set REDIS_PASSWORD=<Primary Access Key>
137
-
```
138
64
139
-
1. In the terminal window, change to the correct folder. For example:
140
65
141
-
```console
142
-
cd "C:\git-samples\azure-redis-cache-go-quickstart"
143
-
```
144
-
145
-
1. In the terminal, run the following command to start the application.
146
-
147
-
```console
148
-
go run main.go
149
-
```
150
-
151
-
The HTTP server will start on port `8080`.
152
-
153
-
## Test the application
154
-
155
-
1. Create a few user entries. The below example uses curl:
156
-
157
-
```bash
158
-
curl -i -X POST -d '{"id":"1","name":"foo1", "email":"[email protected]"}' localhost:8080/users/
159
-
curl -i -X POST -d '{"id":"2","name":"foo2", "email":"[email protected]"}' localhost:8080/users/
160
-
curl -i -X POST -d '{"id":"3","name":"foo3", "email":"[email protected]"}' localhost:8080/users/
161
-
```
162
-
163
-
1. Fetch an existing user with its `id`:
164
-
165
-
```bash
166
-
curl -i localhost:8080/users/1
167
-
```
168
-
169
-
You should get JSON response as such:
170
-
171
-
```json
172
-
{
173
-
"email": "foo1@bar",
174
-
"id": "1",
175
-
"name": "foo1"
176
-
}
177
-
```
178
-
179
-
1. If you try to fetch a user who doesn't exist, you get an HTTP `404`. For example:
In this quickstart, you learned how to get started using Go with Azure Cache for Redis. You configured and ran a simple REST API-based application to create and get user information backed by a Redis `HASH` data structure.
70
+
## Related content
196
71
197
-
> [!div class="nextstepaction"]
198
-
> [Create a simple ASP.NET web app that uses an Azure Cache for Redis.](web-app-cache-howto.md)
72
+
[Create a simple ASP.NET web app that uses an Azure Cache for Redis.](web-app-cache-howto.md)
73
+
<!-- Link to Redis Extension for connecting -->
74
+
<!-- Link to any Redis code sample on their site that are germane -->
0 commit comments