Skip to content

Commit 2b96940

Browse files
committed
Simplified layout
1 parent 554d8c3 commit 2b96940

File tree

1 file changed

+21
-145
lines changed

1 file changed

+21
-145
lines changed

articles/redis/go-get-started.md

Lines changed: 21 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -11,188 +11,64 @@ ms.custom:
1111
appliesto:
1212
- ✅ Azure Cache for Redis
1313
ms.devlang: golang
14+
zone_pivot_groups: redis-type
1415
---
1516

16-
# Quickstart: Use Azure Cache for Redis with Go
17+
# Quickstart: Use Azure Redis with Go
1718

18-
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.
1920

20-
## Skip to the code on GitHub
21+
<!-- ## Skip to the code on GitHub
2122
2223
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.
2324
25+
We are breaking the connection to this. -->
26+
2427
## Prerequisites
2528

2629
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
2730
- [Go](https://go.dev/doc/install) (preferably version 1.13 or above)
2831
- [Git](https://git-scm.com/downloads)
2932
- An HTTP client such [curl](https://curl.se/)
3033

31-
## Create an Azure Cache for Redis instance
32-
33-
[!INCLUDE [redis-cache-create](~/reusable-content/ce-skilling/azure/includes/azure-cache-for-redis/includes/redis-cache-create.md)]
34-
35-
[!INCLUDE [redis-cache-create](includes/redis-cache-access-keys.md)]
36-
37-
## Review the code (Optional)
38-
39-
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-
func main() {
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).
53-
54-
```go
55-
...
56-
op := &redis.Options{Addr: redisHost, Password: redisPassword, TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12}}
57-
client := redis.NewClient(op)
58-
59-
ctx := context.Background()
60-
err := client.Ping(ctx).Err()
61-
if err != nil {
62-
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).
71-
>
72-
73-
```go
74-
uh := userHandler{client: client}
34+
## Create an Azure Managed Redis instance
7535

76-
router := mux.NewRouter()
77-
router.HandleFunc("/users/", uh.createUser).Methods(http.MethodPost)
78-
router.HandleFunc("/users/{userid}", uh.getUser).Methods(http.MethodGet)
36+
First you must create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal.
7937

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.
39+
- [Azure Managed Redis](includes/managed-redis-create.md)
40+
- [Azure Cache for Redis](/azure/azure-cache-for-redis/quickstart-create-redis)
8241

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.
8443

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.
44+
[!INCLUDE [managed-redis-create](includes/managed-redis-create.md)]
8745

88-
```go
89-
type userHandler struct {
90-
client *redis.Client
91-
}
92-
...
46+
## Code to connect to a AMR Cache
9347

94-
func (uh userHandler) createUser(rw http.ResponseWriter, r *http.Request) {
95-
// details omitted
96-
}
97-
...
48+
## Code to test a connection
9849

99-
func (uh userHandler) getUser(rw http.ResponseWriter, r *http.Request) {
100-
// details omitted
101-
}
102-
```
10350

104-
## Clone the sample application
51+
## Code set a key, get a key
10552

106-
Start by cloning the application from GitHub.
10753

108-
1. Open a command prompt and create a new folder named `git-samples`.
10954

110-
```bash
111-
md "C:\git-samples"
112-
```
11355

114-
1. Open a git terminal window, such as git bash. Use the `cd` command to change to the new folder where you want to clone the sample app.
11556

116-
```bash
117-
cd "C:\git-samples"
118-
```
11957

120-
1. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
12158

122-
```bash
123-
git clone https://github.com/Azure-Samples/azure-redis-cache-go-quickstart.git
124-
```
12559

126-
## Run the application
12760

128-
The application accepts connectivity and credentials in the form of environment variables.
12961

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/)
13162

132-
1. Set them to the respective environment variables:
13363

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-
```
13864

139-
1. In the terminal window, change to the correct folder. For example:
14065

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:
180-
181-
```bash
182-
curl -i localhost:8080/users/100
183-
184-
#response
185-
186-
HTTP/1.1 404 Not Found
187-
Date: Fri, 08 Jan 2021 13:43:39 GMT
188-
Content-Length: 0
189-
```
66+
<!-- clean up resoureces include -->
19067

19168
[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)]
19269

193-
## Next steps
194-
195-
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
19671

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

Comments
 (0)