Skip to content

Commit 757f358

Browse files
authored
Merge pull request #12 from flang-msft/ShawnJackson-patch-2
[AQ] edit pass: cache-aspnetcore-output-cache-provider
2 parents 808d6da + 57d5445 commit 757f358

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

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

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: ASP.NET core Output Cache Provider for Azure Cache for Redis
3-
description: Use the Redis Output Cache Provider to cache ASP.NET core page output out of process with Azure Cache for Redis.
2+
title: ASP.NET Core Output Cache Provider for Azure Cache for Redis
3+
description: Learn how to use the Redis Output Cache Provider to cache ASP.NET Core page output out of process by using Azure Cache for Redis.
44
author: flang-msft
55
ms.author: cawa
66
ms.service: cache
@@ -9,69 +9,70 @@ ms.custom: devx-track-csharp
99
ms.topic: how-to
1010
ms.date: 06/13/2024
1111

12-
13-
#CustomerIntent: As a cloud developer, I want to understand core output caching using Azure Cache for Redis so that I can implement it for storing page output.
12+
#customer intent: As a cloud developer, I want to understand core output caching via Azure Cache for Redis so that I can implement it for storing page output.
1413
---
14+
1515
# ASP.NET Core Output Cache Provider for Azure Cache for Redis
1616

17-
This article explains how to configure Redis output caching middleware in an ASP.NET Core app. The output caching middleware enables caching of HTTP responses. The benefits for using output caching include:
17+
This article explains how to configure Redis output caching middleware in an ASP.NET Core app. The output caching middleware enables caching of HTTP responses. The benefits of using output caching include:
1818

19-
- Saving web server resource utilization from repeatedly generating HTTP responses and rendering HTML web pages.
20-
- Improving web request performance by reducing dependencies calls.
19+
- Saving web server resource utilization from repeatedly generating HTTP responses and rendering HTML webpages.
20+
- Improving web request performance by reducing dependency calls.
2121

2222
The benefits of using Azure Cache for Redis as your output cache include:
2323

24-
- Saving web server memory resource utilization by saving cached content in an external Redis.
25-
- Improving web application resiliency by persisting cached content in an external Redis in the scenarios of server failover or restart.
26-
- The Redis output caching implementation includes performance and storage optimizations comparing with custom implementations of the Output Caching middleware providers.
24+
- Saving web server memory resource utilization by storing cached content in an external Redis database.
25+
- Improving web application resiliency by persisting cached content in an external Redis database in the scenario of server failover or restart.
26+
27+
The Redis output caching implementation includes performance and storage optimizations that compare with custom implementations of the output caching middleware providers.
2728

28-
The output caching middleware can be used in all types of ASP.NET Core apps: Minimal API, Web API with controllers, MVC, and Razor Pages. For a detailed walkthrough on output caching syntax and features, see [Output caching middleware in ASP.NET Core](/aspnet/core/performance/caching/output).
29+
You can use the output caching middleware in all types of ASP.NET Core apps: minimal API, web API with controllers, Model-View-Controller (MVC), and Razor Pages. For a detailed walkthrough of output caching syntax and features, see [Output caching middleware in ASP.NET Core](/aspnet/core/performance/caching/output).
2930

3031
## Prerequisites
3132

32-
- Install .NET 8 SDK or later. [Download](https://dotnet.microsoft.com/download/dotnet/8.0)
33-
- Install Visual Studio Code. [Download](https://code.visualstudio.com/download)
34-
- Create an Azure Cache for Redis instance.
33+
- [Download](https://dotnet.microsoft.com/download/dotnet/8.0) and install .NET 8 SDK or later.
34+
- [Download](https://code.visualstudio.com/download) and install Visual Studio Code.
35+
- Create an Azure Cache for Redis instance. For more information, see:
3536
- [Quickstart: Create an open-source Redis cache](./quickstart-create-redis.md)
3637
- [Quickstart: Create a Redis Enterprise cache](./quickstart-create-redis-enterprise.md)
3738

38-
For an end-to-end application that uses Redis output caching, see [.NET 8 Web Application with Redis Output Caching and Azure OpenAI](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/tutorial/output-cache-open-ai)
39+
For an end-to-end application that uses Redis output caching, see [.NET 8 Web Application with Redis Output Caching and Azure OpenAI](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/tutorial/output-cache-open-ai).
3940

4041
## Add Redis output caching middleware to an empty ASP.NET core web application
4142

4243
Here's the procedure for using Azure Cache for Redis as the output caching middleware in an ASP.NET Core app. The output caching middleware enables caching of HTTP responses as page output.
4344

4445
### Create an empty ASP.NET core web application
4546

46-
Open the command prompt. Use the .NET command line to create an empty ASP.NET core web application.
47+
Open a command prompt. Use the .NET command line to create an empty ASP.NET core web application:
4748

4849
```cmd
4950
mkdir RedisOutputCache
5051
cd RedisOutputCache
5152
dotnet new web
5253
```
5354

54-
### Add Redis Output Caching NuGet Packages
55+
### Add NuGet packages for Redis output caching
5556

56-
Make sure your command prompt is in the project folder where the `*.csproj` file is located. Install the [Microsoft.AspNetCore.OutputCaching.StackExchangeRedis](https://www.nuget.org/packages/Microsoft.AspNetCore.OutputCaching.StackExchangeRedis) NuGet package.
57+
Make sure your command prompt is in the project folder that contains the `*.csproj` file. Install the [Microsoft.AspNetCore.OutputCaching.StackExchangeRedis](https://www.nuget.org/packages/Microsoft.AspNetCore.OutputCaching.StackExchangeRedis) NuGet package:
5758

5859
```cmd
5960
dotnet add package Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
6061
```
6162

62-
### Add Redis Output Caching middleware in the web application startup code
63+
### Add Redis output caching middleware in the web application's startup code
6364

64-
1. Open the project director in Visual Studio Code by typing _Code ._ in the command prompt.
65+
1. Use the command prompt to open the project directory in Visual Studio Code:
6566

6667
```cmd
6768
code .
6869
```
6970
70-
If prompted, select **Yes I trust the author** to proceed.
71+
If you're prompted, select **Yes I trust the author** to proceed.
7172
72-
1. Open _Program.cs_ file.
73+
1. Open the _Program.cs_ file.
7374
74-
1. Add the following function calls. `AddStackExchangeRedisOutputCache()`, `AddOutputCache()`, `UseOutputCache()`.
75+
1. Add the `AddStackExchangeRedisOutputCache()`, `AddOutputCache()`, and `UseOutputCache()` function calls:
7576
7677
```csharp
7778
var builder = WebApplication.CreateBuilder(args);
@@ -96,15 +97,15 @@ dotnet add package Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
9697
9798
```
9899
99-
1. Save the _Program.cs_ file. Make sure the application builds with no error in the Command Prompt.
100+
1. Save the _Program.cs_ file. Make sure the application builds with no errors in the command prompt:
100101
101102
```cmd
102103
dotnet build
103104
```
104105
105106
### Configure one endpoint or page
106107
107-
1. Add the cached endpoint under `app.MetGet()`.
108+
1. Add the cached endpoint under `app.MetGet()`:
108109
109110
```csharp
110111
app.MapGet("/", () => "Hello World!");
@@ -113,31 +114,29 @@ dotnet add package Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
113114
app.MapGet("/cached",()=> "Hello Redis Output Cache" + DateTime.Now).CacheOutput();
114115
```
115116
116-
1. Save the _program.cs_ file. Make sure the application builds with no error in the Command Prompt.
117+
1. Save the _Program.cs_ file. Make sure the application builds with no errors in the command prompt:
117118
118119
```cmd
119120
dotnet build
120121
```
121122
122-
### Configure Redis Cache connection
123+
### Configure the Redis Cache connection
123124
124-
It's security best practice to not store password in clear text in source-controlled code files. ASP.NET core provides [User Secrets management](/aspnet/core/security/app-secrets) to ensure secrets, such as connection strings, are saved and accessed securely. Use this feature to manage the Azure Cache for Redis connection strings.
125+
It's a security best practice to avoid storing passwords in clear text in source-controlled code files. ASP.NET Core provides [user secrets management](/aspnet/core/security/app-secrets) to help ensure that secrets, such as connection strings, are saved and accessed securely. Use this feature to manage the Azure Cache for Redis connection strings.
125126
126-
1. Enable secret storage using the CLI.
127+
1. Enable the storage of secrets by using the Azure CLI:
127128
128129
```cli
129130
dotnet user-secrets init
130131
```
131132
132-
1. Obtain the Azure Cache for Redis connection strings using the Azure portal.
133+
1. Obtain the Azure Cache for Redis connection strings by using the Azure portal.
133134
134-
The connection string for OSS Azure Cache for Redis can be found by selecting **Authentication** on the Resource menu.
135-
`<Azure_redis_name>.redis.cache.windows.net:6380,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False`
135+
You can find the connection string for Redis OSS by selecting **Authentication** on the **Resource** menu. Here's an example string: `<Azure_redis_name>.redis.cache.windows.net:6380,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False`.
136136
137-
The access keys for Enterprise Redis can found by selecting **Access keys** from the Resource menu. The connection string can be derived with other Redis information from the **Overview** section of the Resource menu.
138-
`<Azure_redis_name>.<Azure_region>.redisenterprise.cache.azure.net:10000,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False`
137+
You can find the access keys for Redis Enterprise by selecting **Access keys** on the **Resource** menu. The connection string can be derived with other Redis information from the **Overview** section of the **Resource** menu. Here's an example string: `<Azure_redis_name>.<Azure_region>.redisenterprise.cache.azure.net:10000,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False`.
139138
140-
1. Set the Redis connection for the web application using the CLI
139+
1. Set the Redis connection for the web application by using the Azure CLI:
141140
142141
```cli
143142
dotnet user-secrets set RedisCacheConnection <Azure_Redis_ConnectionString>
@@ -161,12 +160,11 @@ It's security best practice to not store password in clear text in source-contro
161160
...
162161
```
163162
164-
### Verify the Redis output cache is working
163+
### Verify that the Redis output cache is working
165164
166-
1. Browse to the local host URL, for example:
167-
`http://localhost:5063/cached`
165+
1. Browse to the local host URL. Here's an example: `http://localhost:5063/cached`.
168166
169-
2. Observe if the current time is being displayed. Refreshing the browser doesn't change the time because the content is cached. The following example of text might be displayed by the /cached endpoint.
167+
1. Observe if the current time is being displayed. Refreshing the browser doesn't change the time because the content is cached. The `/cached` endpoint might display text like the following example:
170168
171169
```cmd
172170
Hello Redis Output Cache5/27/2024 8:31:35 PM

0 commit comments

Comments
 (0)