Skip to content

Commit 0c46ab9

Browse files
New Cache tutorial (#293)
* remove connection string (will add in note at bottom later) and reorder tutorial * add a quick note about the connection string * Apply suggestions from code review * Update caching-components.md Fix identation * Update caching-components.md --------- Co-authored-by: David Pine <[email protected]>
1 parent ff10716 commit 0c46ab9

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

docs/caching/caching-components.md

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ Visual Studio creates a new .NET Aspire solution that consists of the following
4141
- **AspireRedis.AppHost** - An orchestrator project designed to connect and configure the different projects and services of your app.
4242
- **AspireRedis.ServiceDefaults** - A .NET Aspire shared project to manage configurations that are reused across the projects in your solution related to [resilience](/dotnet/core/resilience/http-resilience), [service discovery](../service-discovery/overview.md), and [telemetry](../telemetry.md).
4343

44+
## Configure the App Host project
45+
46+
Update the _Program.cs_ file of the `AspireRedis.AppHost` project to match the following code:
47+
48+
```csharp
49+
var builder = DistributedApplication.CreateBuilder(args);
50+
51+
var redis = builder.AddRedis("cache");
52+
53+
var apiservice = builder.AddProject<Projects.AspireRedis_ApiService>("apiservice")
54+
.WithReference(redis);
55+
56+
builder.AddProject<Projects.AspireRedis_Web>("webfrontend")
57+
.WithReference(apiservice)
58+
.WithReference(redis);
59+
60+
builder.Build().Run();
61+
```
62+
63+
The preceding code creates a local Redis container instance and configures the UI and API to use the instance automatically for both output and distributed caching. The code also configures communication between the frontend UI and the backend API using service discovery. With .NET Aspire's implicit service discovery, setting up and managing service connections is streamlined for developer productivity. In the context of this tutorial, the feature simplifies how you connect to Redis.
64+
65+
Traditionally, you'd manually specify the Redis connection string in each project's _appsettings.json_ file:
66+
67+
```json
68+
{
69+
"ConnectionStrings": {
70+
"cache": "localhost:6379"
71+
}
72+
}
73+
```
74+
75+
Configuring connection string with this method, while functional, requires duplicating the connection string across multiple projects, which can be cumbersome and error-prone.
76+
4477
## Configure the UI with output caching
4578

4679
1. Add the [.NET Aspire StackExchange Redis output caching](stackexchange-redis-output-caching-component.md) component packages to your `AspireStorage` app:
@@ -60,14 +93,6 @@ dotnet add package Aspire.StackExchange.Redis.OutputCaching --prerelease
6093
- Configures ASP.NET Core output caching to use a Redis instance with the specified connection name.
6194
- Automatically enables corresponding health checks, logging, and telemetry.
6295

63-
1. In the _appsettings.json file of the `AspireRedis.Web` project, add the corresponding connection string information:
64-
65-
```json
66-
"ConnectionStrings": {
67-
"cache": "localhost:6379"
68-
}
69-
```
70-
7196
1. Replace the contents of the _Home.razor_ file of the `AspireRedis.Web` Blazor project with the following:
7297

7398
```razor
@@ -87,24 +112,16 @@ dotnet add package Aspire.StackExchange.Redis.OutputCaching --prerelease
87112

88113
1. Add the [.NET Aspire StackExchange Redis distributed caching](stackexchange-redis-output-caching-component.md) component packages to your `AspireRedis` app:
89114

90-
```dotnetcli
91-
dotnet add package Aspire.StackExchange.Redis.DistributedCaching --prerelease
92-
```
115+
```dotnetcli
116+
dotnet add package Aspire.StackExchange.Redis.DistributedCaching --prerelease
117+
```
93118

94119
1. Towards the top of the _Program.cs_ file, add a call to <xref:Microsoft.Extensions.Hosting.AspireRedisDistributedCacheExtensions.AddRedisDistributedCache%2A>:
95120

96121
```csharp
97122
builder.AddRedisDistributedCache("cache");
98123
```
99124

100-
1. In the _appsettings.json file of the `AspireRedis.ApiService` project, add the corresponding connection string information:
101-
102-
```json
103-
"ConnectionStrings": {
104-
"cache": "localhost:6379"
105-
}
106-
```
107-
108125
1. In the _Program.cs_ file, replace the existing `/weatherforecast` endpoint code with the following:
109126

110127
```csharp
@@ -137,27 +154,6 @@ dotnet add package Aspire.StackExchange.Redis.DistributedCaching --prerelease
137154
.WithName("GetWeatherForecast");
138155
```
139156

140-
## Configure the App Host project
141-
142-
Update the _Program.cs_ file of the `AspireRedis.AppHost` project to match the following code:
143-
144-
```csharp
145-
var builder = DistributedApplication.CreateBuilder(args);
146-
147-
var redis = builder.AddRedis("cache");
148-
149-
var apiservice = builder.AddProject<Projects.AspireRedis_ApiService>("apiservice")
150-
.WithReference(redis);
151-
152-
builder.AddProject<Projects.AspireRedis_Web>("webfrontend")
153-
.WithReference(apiservice)
154-
.WithReference(redis);
155-
156-
builder.Build().Run();
157-
```
158-
159-
The preceding code creates a local Redis container instance and configures the UI and API to use the instance automatically for output and distributed caching. The code also configures communication between the frontend UI and the backend API using discovery.
160-
161157
## Test the app locally
162158

163159
Test the caching behavior of your app using the following steps:

0 commit comments

Comments
 (0)