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
Copy file name to clipboardExpand all lines: articles/azure-cache-for-redis/cache-go-get-started.md
+25-23Lines changed: 25 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,15 @@
1
1
---
2
-
title: Use Azure Cache for Redis with Go
2
+
title: 'Quickstart: Use Azure Cache for Redis with Go'
3
3
description: In this quickstart, you learn how to create a Go app that uses Azure Cache for Redis.
4
4
5
5
6
-
7
6
ms.devlang: golang
8
7
ms.topic: quickstart
9
8
ms.date: 09/09/2021
10
9
ms.custom: mode-api, devx-track-go
11
10
---
12
11
13
-
# Quickstart: Use Azure Cache for Redis with Go
12
+
# Quickstart: Use Azure Cache for Redis with a Go app
14
13
15
14
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](./cache-overview.md).
16
15
@@ -22,24 +21,24 @@ If you want to go straight to the code, see the [Go quickstart sample](https://g
22
21
23
22
## Prerequisites
24
23
25
-
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
24
+
- Azure subscription. [Create one for free](https://azure.microsoft.com/free/)
26
25
-[Go](https://go.dev/doc/install) (preferably version 1.13 or later)
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).
37
+
If you're interested in learning how the code works, you can review the following code snippets. Feel free to skip ahead to [Run the application](#run-the-application).
39
38
40
39
The open source [go-redis](https://github.com/go-redis/redis) library is used to interact with Azure Cache for Redis.
41
40
42
-
The `main` function starts off by reading the host name and password (Access Key) for the Azure Cache for Redis instance.
41
+
The `main` function starts by reading the host name and password (access key) for the Azure Cache for Redis instance.
43
42
44
43
```go
45
44
funcmain() {
@@ -48,7 +47,7 @@ func main() {
48
47
...
49
48
```
50
49
51
-
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](cache-remove-tls-10-11.md).
50
+
Then, you 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](cache-remove-tls-10-11.md).
52
51
53
52
```go
54
53
...
@@ -63,10 +62,10 @@ if err != nil {
63
62
...
64
63
```
65
64
66
-
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.
65
+
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.
67
66
68
67
> [!NOTE]
69
-
>The [gorilla mux library](https://github.com/gorilla/mux) is used for routing (although it's not strictly necessary, and using the standard library for this sample application is an option).
68
+
>The [gorilla mux library](https://github.com/gorilla/mux) is used for routing (although it's not required, and using the standard library for this sample application is an option).
The `userHandler` struct encapsulates a [redis.Client](https://pkg.go.dev/github.com/go-redis/redis/v8#Client). The `createUser` and `getUser` methods use the redis.Client. For brevity, the code for these methods isn't included in this article.
81
+
The `userHandler` struct encapsulates [redis.Client](https://pkg.go.dev/github.com/go-redis/redis/v8#Client). The `createUser` and `getUser` methods use redis.Client. For brevity, the code for these methods isn't included in this article.
83
82
84
83
- `createUser`: Accepts a JSON payload (that has user information) and saves it as a `HASH` in Azure Cache for Redis.
85
84
- `getUser`: Fetches user info from `HASH` or returns an HTTP `404` response if it's not found.
@@ -126,22 +125,24 @@ Start by cloning the application on GitHub:
126
125
127
126
The application accepts connectivity and credentials in the form of environment variables.
128
127
129
-
1. In the [Azure portal](https://portal.azure.com/), get the host name and access keys for the Azure Cache for Redis instance.
128
+
1. In the [Azure portal](https://portal.azure.com/), get the host name and access key for the instance of Azure Cache for Redis.
130
129
131
-
1. Set them to the respective environment variables:
130
+
1. Set the host name and access key to the following environment variables:
132
131
133
132
```console
134
133
set REDIS_HOST=<Host name>:<port> (for example, <name of cache>.redis.cache.windows.net:6380)
135
134
set REDIS_PASSWORD=<Primary Access Key>
136
135
```
137
136
138
-
1. In the terminal window, change to the correct folder. For example:
137
+
1. In the terminal, go to the folder you created for the samples:
138
+
139
+
For example:
139
140
140
141
```console
141
142
cd "C:\git-samples\azure-redis-cache-go-quickstart"
142
143
```
143
144
144
-
1. In the terminal, run the following command to start the application.
145
+
1. In the terminal, start the application by using this command:
145
146
146
147
```console
147
148
go run main.go
@@ -151,23 +152,23 @@ The HTTP server starts on port `8080`.
151
152
152
153
## Test the application
153
154
154
-
1. Create a few user entries.
155
+
1. Create a few user entries.
155
156
156
-
The following example uses cURL:
157
+
The following example uses curl:
157
158
158
159
```bash
159
160
curl -i -X POST -d '{"id":"1","name":"foo1", "email":"[email protected]"}' localhost:8080/users/
160
161
curl -i -X POST -d '{"id":"2","name":"foo2", "email":"[email protected]"}' localhost:8080/users/
161
162
curl -i -X POST -d '{"id":"3","name":"foo3", "email":"[email protected]"}' localhost:8080/users/
162
163
```
163
164
164
-
1. Fetch an existing user by using the value for the `id`:
165
+
1. Fetch an existing user by using the value for `id`:
165
166
166
167
```bash
167
168
curl -i localhost:8080/users/1
168
169
```
169
170
170
-
The output is JSON response that is similar to this example:
171
+
The output is a JSON response that's similar to this example:
171
172
172
173
```json
173
174
{
@@ -177,7 +178,7 @@ The HTTP server starts on port `8080`.
177
178
}
178
179
```
179
180
180
-
1. If you try to fetch a user who doesn't exist, you get an HTTP `404`.
181
+
1. If you try to fetch a user who doesn't exist, you get an HTTP `404` error message.
181
182
182
183
For example:
183
184
@@ -199,5 +200,6 @@ The HTTP server starts on port `8080`.
199
200
200
201
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.
201
202
202
-
> [!div class="nextstepaction"]
203
-
> [Create a simple ASP.NET web app that uses an Azure Cache for Redis.](./cache-web-app-howto.md)
203
+
## Related content
204
+
205
+
- [Create a simple ASP.NET web app that uses Azure Cache for Redis.](./cache-web-app-howto.md)
# Quickstart: Use Azure Cache for Redis in a Java app
14
14
15
15
In this quickstart, you incorporate Azure Cache for Redis into a Java app by using the [Jedis](https://github.com/xetorthio/jedis) Redis client. Your cache is a secure, dedicated cache that is accessible from any application within Azure.
16
16
17
17
## Skip to the code
18
18
19
-
This article describes how to create an app by using the Azure portal and then modify the code to end up with a working sample app.
19
+
This quickstart uses the Maven archetype feature to generate scaffolding for the app. The quickstart describes how to modify the generated code to end up with a working sample app.
20
20
21
21
If you want to go straight to the code, see the [Java quickstart sample](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/java) on GitHub.
22
22
@@ -33,7 +33,7 @@ If you want to go straight to the code, see the [Java quickstart sample](https:/
33
33
34
34
## Set up the working environment
35
35
36
-
Depending on your operating system, add environment variables for your **Host name** and **Primary access key** that you noted previously. Open a command prompt, or a terminal window, and set up the following values:
36
+
Depending on your operating system, add environment variables for host name and primary access key that you noted earrlier. In a command prompt or a terminal window, set up the following values:
37
37
38
38
### [Linux](#tab/bash)
39
39
@@ -53,16 +53,16 @@ set REDISCACHEKEY=<your-primary-access-key>
53
53
54
54
Replace the placeholders with the following values:
55
55
56
-
-`<your-host-name>`: The DNS host name, obtained from the *Properties* section of your Azure Cache for Redis resource in the Azure portal.
57
-
-`<your-primary-access-key>`: The primary access key, obtained from the *Access keys* section of your Azure Cache for Redis resource in the Azure portal.
56
+
-`<your-host-name>`: The DNS host name, obtained from the **Properties** section of your Azure Cache for Redis resource in the Azure portal.
57
+
-`<your-primary-access-key>`: The primary access key, obtained from the **Access keys** section of your Azure Cache for Redis resource in the Azure portal.
58
58
59
59
## Understand the Java sample
60
60
61
61
In this sample, you use Maven to run the quickstart app.
62
62
63
-
1.Change to the new *redistest* project directory.
63
+
1.Go to the new *redistest* project directory.
64
64
65
-
1. Open the *pom.xml* file. In the file, you see a dependency for [Jedis](https://github.com/xetorthio/jedis):
65
+
1. Open the *pom.xml* file. In the file, verify that a dependency for [Jedis](https://github.com/xetorthio/jedis) appears:
66
66
67
67
```xml
68
68
<dependency>
@@ -76,7 +76,7 @@ In this sample, you use Maven to run the quickstart app.
76
76
77
77
1. Close the *pom.xml* file.
78
78
79
-
1. Open *App.java* and see the code with the following code:
79
+
1. Open *App.java* and verify that the following code appears:
80
80
81
81
```java
82
82
package example.demo;
@@ -103,7 +103,7 @@ In this sample, you use Maven to run the quickstart app.
103
103
.ssl(useSsl)
104
104
.build());
105
105
106
-
// Perform cache operations using the cache connection object...
106
+
// Perform cache operations by using the cache connection object.
107
107
108
108
// Simple PING command
109
109
System.out.println( "\nCache Command : Ping" );
@@ -129,13 +129,13 @@ In this sample, you use Maven to run the quickstart app.
129
129
}
130
130
```
131
131
132
-
This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed.
132
+
This code shows you how to connect to an Azure Cache for Redis instance by using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The `PING` and `CLIENT LIST` commands are also executed.
133
133
134
-
1. Close the *App.java*.
134
+
1. Close *App.java*.
135
135
136
136
## Build and run the app
137
137
138
-
1. First, if you haven't already, you must set the environment variables as noted previously.
138
+
1. Set the environment variables as noted earlier:
139
139
140
140
### [Linux](#tab/bash)
141
141
@@ -153,7 +153,7 @@ In this sample, you use Maven to run the quickstart app.
153
153
154
154
---
155
155
156
-
1.Execute the following Maven command to build and run the app:
156
+
1.To build and run the app, run the following Maven command:
157
157
158
158
### [Linux](#tab/bash)
159
159
@@ -171,7 +171,7 @@ In this sample, you use Maven to run the quickstart app.
171
171
172
172
---
173
173
174
-
In the following output, you can see that the `Message` key previously had a cached value. The value was updated to a new value using `jedis.set`. The app also executed the `PING` and `CLIENT LIST` commands.
174
+
In the following output, you can see that the `Message` key previously had a cached value. The value was updated to a new value by using `jedis.set`. The app also executed the `PING` and `CLIENT LIST` commands.
In this quickstart, you learned how to use Azure Cache for Redis from a Java application. Continue to the next quickstart to use Azure Cache for Redis with an ASP.NET web app.
0 commit comments