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
# Quickstart: Use Azure Cache for Redis in .NET Core
13
14
14
15
In this quickstart, you incorporate Azure Cache for Redis into a .NET Core app to have access to a secure, dedicated cache that is accessible from any application within Azure. You specifically use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client with C# code in a .NET Core console app.
15
16
16
-
## Skip to the code on GitHub
17
+
## Skip to the code
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.
17
20
18
-
Clone the repo [https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core) on GitHub.
21
+
If you want to go straight to the code, see the [.NET Core sample](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core) on GitHub.
19
22
20
23
## Prerequisites
21
24
@@ -28,33 +31,33 @@ Clone the repo [https://github.com/Azure-Samples/azure-cache-redis-samples/tree/
Make a note of the **HOST NAME** and the **Primary** access key. You'll use these values later to construct the *CacheConnection* secret.
34
+
Make a note of the values for **HOST NAME** and the **Primary** access key. You'll use these values later to construct the `CacheConnection` secret.
32
35
33
36
## Add a local secret for the connection string
34
37
35
-
In your command window, execute the following command to store a new secret named *CacheConnection*, after replacing the placeholders (including angle brackets) for your cache name and primary access key:
38
+
In your command window, execute the following command to store a new secret named *CacheConnection* after you replac the placeholders (including angle brackets) with your cache name (`<cache name>`) and primary access key (`<primary-access-key>`):
36
39
37
40
```dos
38
41
dotnet user-secrets set CacheConnection "<cache name>.redis.cache.windows.net,abortConnect=false,ssl=true,allowAdmin=true,password=<primary-access-key>"
39
42
```
40
43
41
-
## Connect to the cache with RedisConnection
44
+
## Connect to the cache by using RedisConnection
42
45
43
-
The connection to your cache is managed by the `RedisConnection` class. The connection is first made in this statement from `Program.cs`:
46
+
The connection to your cache is managed by the `RedisConnection` class. First, you make the connection in this statement from *Program.cs*:
In `RedisConnection.cs`, you see the `StackExchange.Redis` namespace has been added to the code. This is needed for the `RedisConnection` class.
53
+
In *RedisConnection.cs*, the `StackExchange.Redis` namespace has been added to the code. The namespace is required for the `RedisConnection` class.
51
54
52
55
```csharp
53
56
usingStackExchange.Redis;
54
57
55
58
```
56
-
<!-- Is this right Philo -->
57
-
The `RedisConnection` code ensures that there is always a healthy connection to the cache by managing the `ConnectionMultiplexer` instance from `StackExchange.Redis`. The `RedisConnection` class recreates the connection when a connection is lost and unable to reconnect automatically.
59
+
60
+
The `RedisConnection` code ensures that there is always a healthy connection to the cache by managing the `ConnectionMultiplexer` instance from `StackExchange.Redis`. The `RedisConnection` class re-creates the connection when a connection is lost and unable to reconnect automatically.
58
61
59
62
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
60
63
@@ -63,7 +66,9 @@ For more information, see [StackExchange.Redis](https://stackexchange.github.io/
63
66
## Executing cache commands
64
67
65
68
In `program.cs`, you can see the following code for the `RunRedisCommandsAsync` method in the `Program` class for the console application:
69
+
66
70
<!-- Replaced this code with lines 57-81 from dotnet-core/Program.cs -->
Cache items can be stored and retrieved by using the `StringSetAsync` and `StringGetAsync` methods.
112
+
You can store and retrieve cache items by using the `StringSetAsync` and `StringGetAsync` methods.
108
113
109
-
In the example, you can see the `Message` key is set to value. The app updated that cached value. The app also executed the `PING` and command.
114
+
In the example, you can see the `Message` key is set to a value. The app updated that cached value. The app also executed the `PING` and command.
110
115
111
116
### Work with .NET objects in the cache
112
117
113
-
The Redis server stores most data as strings, but these strings can contain many types of data, including serialized binary data, which can be used when storing .NET objects in the cache.
118
+
The Redis server stores most data in string format. The strings can contain many types of data, including serialized binary data. You can use serialized binary data when you store .NET objects in the cache.
114
119
115
-
Azure Cache for Redis can cache both .NET objects and primitive data types, but before a .NET object can be cached it must be serialized.
120
+
Azure Cache for Redis can cache both .NET objects and primitive data types, but before a .NET object can be cached, it must be serialized.
116
121
117
-
This .NET object serialization is the responsibility of the application developer, and gives the developer flexibility in the choice of the serializer.
122
+
The .NET object serialization is the responsibility of the application developer. The object serialization gives the developer flexibility in their choice of the serializer.
118
123
119
-
The following `Employee` class was defined in *Program.cs* so that the sample could also show how to get and set a serialized object:
124
+
The following `Employee` class was defined in *Program.cs* so that the sample could also show how to get and set a serialized object:
120
125
121
126
```csharp
122
127
classEmployee
@@ -136,7 +141,7 @@ class Employee
136
141
137
142
## Run the sample
138
143
139
-
If you have opened any files, save them and build the app with the following command:
144
+
If you opened any files, save the files. Then, build the app by using the following command:
If you continue to use this quickstart, you can keep the resources you created and reuse them.
156
-
157
-
Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.
158
-
159
-
> [!IMPORTANT]
160
-
> Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually on the left instead of deleting the resource group.
161
-
>
162
-
### To delete a resource group
163
-
164
-
1. Sign in to the [Azure portal](https://portal.azure.com) and select **Resource groups**.
165
-
166
-
1. In the **Filter by name...** textbox, type the name of your resource group. The instructions for this article used a resource group named *TestResources*. On your resource group in the result list, select **...** then **Delete resource group**.
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
16
17
-
## Skip to the code on GitHub
17
+
## Skip to the code
18
18
19
-
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.
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.
20
+
21
+
If you want to go straight to the code, see the [Go quickstart sample](https://github.com/Azure-Samples/azure-redis-cache-go-quickstart/) on GitHub.
20
22
21
23
## Prerequisites
22
24
23
25
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
24
-
-[Go](https://go.dev/doc/install) (preferably version 1.13 or above)
26
+
-[Go](https://go.dev/doc/install) (preferably version 1.13 or later)
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
67
66
68
> [!NOTE]
67
-
> [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).
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).
`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.
82
+
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
83
82
-
- `createUser`: accepts a JSON payload (containing user information) and saves it as a `HASH` in Azure Cache for Redis.
83
-
- `getUser`: fetches user info from `HASH` or returns an HTTP `404` response if not found.
84
+
- `createUser`: Accepts a JSON payload (that has user information) and saves it as a `HASH` in Azure Cache for Redis.
85
+
- `getUser`: Fetches user info from `HASH` or returns an HTTP `404` response if it's not found.
1. Open a command prompt and create a new folder named `git-samples`.
107
+
1. At the command prompt for your computer's root directory, create a new folder named `git-samples`:
106
108
107
109
```bash
108
110
md "C:\git-samples"
109
111
```
110
112
111
-
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.
113
+
1. Open a git terminal window, such as by using Git Bash. Use the `cd` command to go to the new folder to clone the sample app.
112
114
113
115
```bash
114
116
cd "C:\git-samples"
@@ -124,12 +126,12 @@ Start by cloning the application from GitHub.
124
126
125
127
The application accepts connectivity and credentials in the form of environment variables.
126
128
127
-
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/)
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
130
129
131
1. Set them to the respective environment variables:
130
132
131
133
```console
132
-
set REDIS_HOST=<Host name>:<port> (e.g. <name of cache>.redis.cache.windows.net:6380)
134
+
set REDIS_HOST=<Host name>:<port> (for example, <name of cache>.redis.cache.windows.net:6380)
133
135
set REDIS_PASSWORD=<Primary Access Key>
134
136
```
135
137
@@ -145,25 +147,27 @@ The application accepts connectivity and credentials in the form of environment
145
147
go run main.go
146
148
```
147
149
148
-
The HTTP server will start on port `8080`.
150
+
The HTTP server starts on port `8080`.
149
151
150
152
## Test the application
151
153
152
-
1. Create a few user entries. The below example uses curl:
154
+
1. Create a few user entries.
155
+
156
+
The following example uses cURL:
153
157
154
158
```bash
155
159
curl -i -X POST -d '{"id":"1","name":"foo1", "email":"[email protected]"}' localhost:8080/users/
156
160
curl -i -X POST -d '{"id":"2","name":"foo2", "email":"[email protected]"}' localhost:8080/users/
157
161
curl -i -X POST -d '{"id":"3","name":"foo3", "email":"[email protected]"}' localhost:8080/users/
158
162
```
159
163
160
-
1. Fetch an existing user with its`id`:
164
+
1. Fetch an existing user by using the value for the`id`:
161
165
162
166
```bash
163
167
curl -i localhost:8080/users/1
164
168
```
165
169
166
-
You should get JSON response as such:
170
+
You should get a JSON response that is similar to this example:
167
171
168
172
```json
169
173
{
@@ -173,7 +177,9 @@ The HTTP server will start on port `8080`.
173
177
}
174
178
```
175
179
176
-
1. If you try to fetch a user who doesn't exist, you get an HTTP `404`. For example:
180
+
1. If you try to fetch a user who doesn't exist, you get an HTTP `404`.
181
+
182
+
For example:
177
183
178
184
```bash
179
185
curl -i localhost:8080/users/100
@@ -185,23 +191,11 @@ The HTTP server will start on port `8080`.
185
191
Content-Length: 0
186
192
```
187
193
188
-
## Clean up resources
189
-
190
-
If you're finished with the Azure resource group and resources you created in this quickstart, you can delete them to avoid charges.
191
-
192
-
> [!IMPORTANT]
193
-
> Deleting a resource group is irreversible, and the resource group and all the resources in it are permanently deleted. If you created your Azure Cache for Redis instance in an existing resource group that you want to keep, you can delete just the cache by selecting **Delete** from the cache **Overview** page.
194
-
195
-
To delete the resource group and its Redis Cache for Azure instance:
196
-
197
-
1. From the [Azure portal](https://portal.azure.com), search for and select **Resource groups**.
198
-
1. In the **Filter by name** text box, enter the name of the resource group that contains your cache instance, and then select it from the search results.
199
-
1. On your resource group page, select **Delete resource group**.
200
-
1. Type the resource group name, and then select **Delete**.
194
+
<!-- Clean up include -->
201
195
202
-

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.
Copy file name to clipboardExpand all lines: articles/azure-cache-for-redis/cache-java-get-started.md
+7-21Lines changed: 7 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,14 +16,16 @@ In this quickstart, you incorporate Azure Cache for Redis into a Java app by usi
16
16
17
17
## Skip to the code
18
18
19
-
Clone the repo on GitHub: [Java quickstart](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/java)
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.
20
+
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.
20
22
21
23
## Prerequisites
22
24
23
25
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
If you continue to use the quickstart code, you can keep the resources created in this quickstart and reuse them.
194
-
195
-
Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.
196
-
197
-
> [!IMPORTANT]
198
-
> Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.
199
-
>
200
-
201
-
1. Sign in to the [Azure portal](https://portal.azure.com) and select **Resource groups**.
202
-
203
-
1. In the **Filter by name** textbox, type the name of your resource group. The instructions for this article used a resource group named *TestResources*. On your resource group in the result list, select **...** then **Delete resource group**.
204
-
205
-
:::image type="content" source="media/cache-java-get-started/azure-cache-redis-delete-resource-group.png" alt-text="Screenshot of the Azure portal that shows the Resource groups page with the Delete resource group button highlighted." lightbox="media/cache-java-get-started/azure-cache-redis-delete-resource-group.png":::
206
-
207
-
1. Type the name of your resource group to confirm deletion and then select **Delete**.
193
+
<!-- Clean up include -->
208
194
209
-
After a few moments, the resourcegroup and all of its contained resources are deleted.
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