Skip to content

Commit eff2f89

Browse files
authored
Merge pull request #292350 from MicrosoftDocs/main
12/23/2024 PM Publish
2 parents 4f1d0d7 + 816ecd8 commit eff2f89

22 files changed

+320
-458
lines changed

articles/azure-cache-for-redis/cache-aspnet-output-cache-provider.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ The Redis Output Cache Provider is an out-of-process storage mechanism for outpu
1515

1616
For ASP.NET Core applications, see [Output Caching in ASP.NET core using Redis in .NET 8](/aspnet/core/performance/caching/output?view=aspnetcore-8.0#redis-cache&preserve-view=true).
1717

18-
<!-- This section points to create azure cache for redis instead of amr. Not sure if this article was updated or not? -->
19-
To use the Redis Output Cache Provider, first configure your cache, and then configure your ASP.NET application using the Redis Output Cache Provider NuGet package. This article provides guidance on configuring your application to use the Redis Output Cache Provider. For more information about creating and configuring an Azure Managed Redis (preview) instance, see [Create a cache](cache-dotnet-how-to-use-azure-redis-cache.md#create-a-cache).
18+
To use the Redis Output Cache Provider, first configure your cache, and then configure your ASP.NET application using the Redis Output Cache Provider NuGet package. This article provides guidance on configuring your application to use the Redis Output Cache Provider.
2019

2120
## Store ASP.NET core page output in Redis
2221

articles/azure-cache-for-redis/cache-development-faq.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ sections:
5151
### Timeout values
5252
* Consider your workload and set the values to match. If you're storing large values, set the timeout to a higher value.
5353
* Set `AbortOnConnectFail` to false and let StackExchange.Redis reconnect for you.
54-
* Use a single, long-lived `ConnectionMultiplexer` instance rather than creating a new connection for each request. For an example of how to manage a connection, see the `RedisConnection`` class in [Connect to the cache with RedisConnection](cache-dotnet-how-to-use-azure-redis-cache.md#connect-to-the-cache-with-redisconnection).
54+
* Use a single, long-lived `ConnectionMultiplexer` instance rather than creating a new connection for each request.
5555
* Set the `ConnectionMultiplexer.ClientName` property to an app instance unique name for diagnostic purposes.
5656
* Use multiple `ConnectionMultiplexer` instances for custom workloads.
5757
* You can follow this model if you have varying load in your application. For example:

articles/azure-cache-for-redis/cache-dotnet-core-quickstart.md

Lines changed: 63 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
title: 'Quickstart: Use Azure Cache for Redis in .NET Core'
33
description: In this quickstart, learn how to access Azure Cache for Redis in your .NET Core apps
44

5-
6-
75
ms.devlang: csharp
86
ms.custom: devx-track-csharp, mvc, mode-other, devx-track-dotnet, ignite-2024
97
ms.topic: quickstart
10-
ms.date: 03/25/2022
8+
ms.date: 12/20/2024
9+
zone_pivot_groups: redis-type
10+
#Customer intent: As a .NET developer, new to Azure Redis, I want to create a new Node.js app that uses Azure Managed Redis or Azure Cache for Redis.
1111
---
12-
# Quickstart: Use Azure Cache for Redis in .NET Core
12+
13+
# Quickstart: Use Azure Redis in .NET Core
1314

1415
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.
1516

@@ -22,126 +23,99 @@ Clone the repo [https://github.com/Azure-Samples/azure-cache-redis-samples/tree/
2223
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
2324
- [.NET Core SDK](https://dotnet.microsoft.com/download)
2425

25-
## Create a cache
26+
::: zone pivot="azure-managed-redis"
27+
28+
## Create an Azure Managed Redis (preview) instance
29+
30+
[!INCLUDE [managed-redis-create](includes/managed-redis-create.md)]
31+
32+
::: zone-end
33+
34+
::: zone pivot="azure-cache-redis"
35+
36+
## Create an Azure Cache for Redis instance
2637

2738
[!INCLUDE [redis-cache-create](~/reusable-content/ce-skilling/azure/includes/azure-cache-for-redis/includes/redis-cache-create.md)]
2839

29-
[!INCLUDE [cache-entra-access](includes/cache-entra-access.md)]
40+
::: zone-end
3041

31-
Make a note of the **HOST NAME**. You'll use these values later to for *appsettings.json*.
42+
[!INCLUDE [cache-entra-access](includes/cache-entra-access.md)]
3243

33-
## Add a local secret for the connection string
44+
### Install the Library for using Microsoft Entra ID Authentication
3445

35-
In your *appsettings.json* file, add the following:
46+
The [Azure.StackExchange.Redis](https://www.nuget.org/packages/Microsoft.Azure.StackExchangeRedis) library contains the Microsoft Entra ID authentication method for connecting to Azure Redis services using Microsoft Entra ID. It's applicable to all Azure Cache for Redis, Azure Cache for Redis Enterprise, and Azure Managed Redis (Preview).
3647

37-
```json
38-
{
39-
"RedisHostName": "your_Azure_Redis_hostname"
40-
}
48+
```cli
49+
dotnet add package Microsoft.Azure.StackExchangeRedis
4150
```
4251

43-
1. Replace "your_Azure_Redis_hostname" with your Azure Redis host name and port numbers. For example: `cache-name.region.redis.azure.net:10000` for Azure Managed Redis (preview), and `cache-name.redis.cache.windows.net:6380` for Azure Cache for Redis services.
52+
---
4453

45-
1. Save the file.
54+
## Connect to the cache using Microsoft Entra ID
4655

47-
## Connect to the cache with RedisConnection
56+
1. Include the libraries in your code
4857

49-
In `RedisConnection.cs`, you see the `StackExchange.Redis` namespace has been added to the code. This is needed for the `RedisConnection` class.
58+
```csharp
59+
using Azure.Identity;
60+
using StackExchange.Redis
61+
```
5062

51-
```csharp
52-
using StackExchange.Redis;
63+
1. Using the default Azure credentials to authenticate the client connection. This enables your code to use the signed-in user credential when running locally, and an Azure managed identity when running in Azure without code change.
5364

65+
```csharp
66+
var configurationOptions = await ConfigurationOptions.Parse($"{_redisHostName}").ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
67+
ConnectionMultiplexer _newConnection = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
68+
IDatabase Database = _newConnection.GetDatabase();
5469
```
55-
<!-- Is this right Philo -->
56-
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.
5770

58-
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).
71+
::: zone pivot="azure-managed-redis"
5972

60-
<!-- :::code language="csharp" source="~/samples-cache/quickstart/dotnet-core/RedisConnection.cs"::: -->
73+
### To edit the _appsettings.json_ file
6174

62-
## Executing cache commands
75+
1. Edit the _Web.config_ file. Then add the following content:
6376

64-
In `program.cs`, you can see the following code for the `RunRedisCommandsAsync` method in the `Program` class for the console application:
65-
<!-- Replaced this code with lines 57-81 from dotnet-core/Program.cs -->
66-
```csharp
67-
private static async Task RunRedisCommandsAsync(string prefix)
68-
{
69-
// Simple PING command
70-
Console.WriteLine($"{Environment.NewLine}{prefix}: Cache command: PING");
71-
RedisResult pingResult = await _redisConnection.BasicRetryAsync(async (db) => await db.ExecuteAsync("PING"));
72-
Console.WriteLine($"{prefix}: Cache response: {pingResult}");
73-
74-
// Simple get and put of integral data types into the cache
75-
string key = "Message";
76-
string value = "Hello! The cache is working from a .NET console app!";
77-
78-
Console.WriteLine($"{Environment.NewLine}{prefix}: Cache command: GET {key} via StringGetAsync()");
79-
RedisValue getMessageResult = await _redisConnection.BasicRetryAsync(async (db) => await db.StringGetAsync(key));
80-
Console.WriteLine($"{prefix}: Cache response: {getMessageResult}");
81-
82-
Console.WriteLine($"{Environment.NewLine}{prefix}: Cache command: SET {key} \"{value}\" via StringSetAsync()");
83-
bool stringSetResult = await _redisConnection.BasicRetryAsync(async (db) => await db.StringSetAsync(key, value));
84-
Console.WriteLine($"{prefix}: Cache response: {stringSetResult}");
85-
86-
Console.WriteLine($"{Environment.NewLine}{prefix}: Cache command: GET {key} via StringGetAsync()");
87-
getMessageResult = await _redisConnection.BasicRetryAsync(async (db) => await db.StringGetAsync(key));
88-
Console.WriteLine($"{prefix}: Cache response: {getMessageResult}");
89-
90-
// Store serialized object to cache
91-
Employee e007 = new Employee("007", "Davide Columbo", 100);
92-
stringSetResult = await _redisConnection.BasicRetryAsync(async (db) => await db.StringSetAsync("e007", JsonSerializer.Serialize(e007)));
93-
Console.WriteLine($"{Environment.NewLine}{prefix}: Cache response from storing serialized Employee object: {stringSetResult}");
94-
95-
// Retrieve serialized object from cache
96-
getMessageResult = await _redisConnection.BasicRetryAsync(async (db) => await db.StringGetAsync("e007"));
97-
Employee e007FromCache = JsonSerializer.Deserialize<Employee>(getMessageResult);
98-
Console.WriteLine($"{prefix}: Deserialized Employee .NET object:{Environment.NewLine}");
99-
Console.WriteLine($"{prefix}: Employee.Name : {e007FromCache.Name}");
100-
Console.WriteLine($"{prefix}: Employee.Id : {e007FromCache.Id}");
101-
Console.WriteLine($"{prefix}: Employee.Age : {e007FromCache.Age}{Environment.NewLine}");
102-
}
77+
```json
78+
"_redisHostName":"<cache-hostname>"
79+
```
10380

104-
```
81+
1. Replace `<cache-hostname>` with your cache host name as it appears in the Overview section of the Resource menu in the Azure portal.
10582

106-
Cache items can be stored and retrieved by using the `StringSetAsync` and `StringGetAsync` methods.
83+
For example, with Azure Managed Redis or the Enterprise tiers: _my-redis.eastus.azure.net:10000_
10784

108-
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.
85+
1. Save the file.
10986

110-
### Work with .NET objects in the cache
87+
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).
11188

112-
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.
89+
::: zone-end
11390

114-
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.
91+
::: zone pivot="azure-cache-redis"
11592

116-
This .NET object serialization is the responsibility of the application developer, and gives the developer flexibility in the choice of the serializer.
93+
### To edit the _appsettings.json_ file
11794

118-
The following `Employee` class was defined in *Program.cs* so that the sample could also show how to get and set a serialized object:
95+
1. Edit the _appsettings.json_ file. Then add the following content:
11996

120-
```csharp
121-
class Employee
122-
{
123-
public string Id { get; set; }
124-
public string Name { get; set; }
125-
public int Age { get; set; }
126-
127-
public Employee(string id, string name, int age)
128-
{
129-
Id = id;
130-
Name = name;
131-
Age = age;
132-
}
133-
}
134-
```
97+
```json
98+
"_redisHostName":"<cache-hostname>"
99+
```
100+
101+
1. Replace `<cache-hostname>` with your cache host name as it appears in the Overview section of the Resource menu in the Azure portal.
102+
103+
For example, with Azure Cache for Redis: _my-redis.eastus.azure.net:6380_
104+
105+
1. Save the file.
106+
107+
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).
108+
::: zone-end
135109

136110
## Run the sample
137111

138-
If you have opened any files, save them and build the app with the following command:
112+
If you opened any files, save them, and build the app with the following command:
139113

140114
```dos
141115
dotnet build
142116
```
143117

144-
Run the app with the following command to test serialization of .NET objects:
118+
To test serialization of .NET objects, run the app with the following command:
145119

146120
```dos
147121
dotnet run
@@ -151,7 +125,7 @@ dotnet run
151125

152126
[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)]
153127

154-
## Next steps
128+
## Related content
155129

156130
- [Connection resilience](cache-best-practices-connection.md)
157131
- [Best Practices Development](cache-best-practices-development.md)

0 commit comments

Comments
 (0)