Skip to content

Light Freshness Edit: ASP.NET - Caching#37087

Open
GitHubber17 wants to merge 4 commits intodotnet:mainfrom
GitHubber17:568764-i
Open

Light Freshness Edit: ASP.NET - Caching#37087
GitHubber17 wants to merge 4 commits intodotnet:mainfrom
GitHubber17:568764-i

Conversation

@GitHubber17
Copy link
Copy Markdown
Contributor

@GitHubber17 GitHubber17 commented May 1, 2026

@GitHubber17 GitHubber17 marked this pull request as ready for review May 1, 2026 06:26
@GitHubber17 GitHubber17 requested a review from tdykstra as a code owner May 1, 2026 06:26
@GitHubber17
Copy link
Copy Markdown
Contributor Author

@tdykstra - Refreshed topics for your review. See the top comment for details.

Cc @cmastr @wadepickett

Comment thread aspnetcore/performance/caching/output.md Outdated
Comment thread aspnetcore/performance/caching/response.md Outdated
Comment thread aspnetcore/performance/caching/response.md Outdated
Comment thread aspnetcore/performance/caching/distributed.md Outdated
Copy link
Copy Markdown
Contributor

@wadepickett wadepickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed. Great work! A few minor items suggested.
I will wait for @tdykstra to review before this is merged since he owns these topics.

The output caching middleware can be used in all types of ASP.NET Core apps: [Minimal APIs](xref:fundamentals/minimal-apis), [Web API with controllers](xref:web-api/index), [MVC](xref:mvc/overview), and [Razor Pages](xref:razor-pages/index). Code examples are provided for Minimal APIs and controller-based APIs. The controller-based API examples show how to use attributes to configure caching. These attributes can also be used in MVC and Razor Pages apps.

The code examples refer to a [Gravatar class](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/performance/caching/output/samples/7.x/Gravatar.cs) that generates an image and provides a "generated at" date and time. The class is defined and used only in [the sample app](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/performance/caching/output/samples/7.x). Its purpose is to make it easy to see when cached output is being used. For more information, see [How to download a sample](xref:fundamentals/index#how-to-download-a-sample) and [Preprocessor directives in sample code](xref:index#preprocessor-directives-in-sample-code).
The code examples refer to a [Gravatar class](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/performance/caching/output/samples/7.x/Gravatar.cs) that generates an image and provides a "generated at" date and time. The class is defined and used only in [the sample app](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/performance/caching/output/samples/7.x). Its purpose is to make it easy to see when cached output is being used. For more information, see [How to download a sample](xref:fundamentals/index#how-to-download-a-sample) and [Preprocessor directives](/dotnet/csharp/fundamentals/program-structure/preprocessor-directives).
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wadepickett - The <xref:index#preprocessor-directives-in-sample-code> link redirects to the top of the Overview page, which provides no information about preprocessor directives. I tried to find a replacement. Can you identify a more appropriate link?

Co-authored-by: Wade Pickett <wpickett@microsoft.com>
@GitHubber17 GitHubber17 requested a review from wadepickett May 1, 2026 17:02
@wadepickett
Copy link
Copy Markdown
Contributor

wadepickett commented May 1, 2026

I deleted my suggestion for the link change for preprocessor-directives, but that also deleted the conversation history. So I'll note it here:

The link I suggested resolved to the top of the overview page which made it useless. At some point we deleted that section in the overview. Sorry to mess that up. You aleady had the best link we can provide.

Follow up for me: I will also look through the entire repo to see if that original outdated link is anywhere else and get it fixed. Thanks for finding this issue to begin with!

Copy link
Copy Markdown
Contributor

@wadepickett wadepickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Great work, thanks!

@tdykstra will also provide a review and then merge.

Copy link
Copy Markdown
Contributor

@tdykstra tdykstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
One important update -- there's a new Azure Redis service

Serialization is configured as part of registering the service, with support for type-specific and generalized serializers via the `WithSerializer` and `WithSerializerFactory` methods, chained from the `AddHybridCache` call. By default, the service handles `string` and `byte[]` types internally, and uses the `System.Text.Json` namespace for everything else. `HybridCache` can be configured for other types of serializers, such as protobuf or XML.

To see the relative simplicity of the `HybridCache` API, compare code that uses it to code that uses `IDistributedCache`. Here's an example of what using `IDistributedCache` looks like:
To see the relative simplicity of the `HybridCache` API, compare code that uses it to code that uses the `IDistributedCache` interface. Here's an example of a configuration with the `IDistributedCache` interface:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert line 20 to the original text.

* <xref:performance/caching/hybrid>
* [Hybrid Cache API proposal (`dotnet/aspnetcore` #54647)](https://github.com/dotnet/aspnetcore/issues/54647)
* [`HybridCache` source code](https://source.dot.net/#Microsoft.Extensions.Caching.Abstractions/Hybrid/HybridCache.cs,8c0fe94693d1ac8d) <!--keep-->
* [HybridCache library in ASP.NET Core](xref:performance/caching/hybrid)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original form of link here is fine. It gets the link text from the title of the article.

An app configures the cache implementation with a <xref:Microsoft.Extensions.Caching.StackExchangeRedis.RedisCache> instance by calling the <xref:Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache%2A> method. For [output caching](xref:performance/caching/output#cache-storage), use the <xref:Microsoft.Extensions.DependencyInjection.StackExchangeRedisOutputCacheServiceCollectionExtensions.AddStackExchangeRedisOutputCache%2A> method.

We recommend production apps use the Distributed Redis Cache because it's the most performant. For more information see [Recommendations](#recommendations).
1. Create an Azure Cache for Redis.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Create an Azure Cache for Redis.
1. Create an instance of Azure Cache for Redis.

1. Create an Azure Cache for Redis.

[Redis](https://redis.io/) is an open source in-memory data store, which is often used as a distributed cache. You can configure an [Azure Cache for Redis](/azure/azure-cache-for-redis/cache-overview) for an Azure-hosted ASP.NET Core app, and use an Azure Cache for Redis for local development.
1. Copy the Primary connection string (StackExchange.Redis) to [Configuration](xref:fundamentals/configuration/index).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Copy the Primary connection string (StackExchange.Redis) to [Configuration](xref:fundamentals/configuration/index).
1. Copy the primary connection string (StackExchange.Redis) to [Configuration](xref:fundamentals/configuration/index).

* Azure: Save the connection string in a secure store such as [Azure Key Vault](/azure/key-vault/general/overview)
- **For Azure**: Save the connection string in a secure store such as [Azure Key Vault](/azure/key-vault/general/overview).

The following code enables the Azure Cache for Redis:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change Azure Cache for Redis to Azure Managed Redis and link to
the Azure Managed Redis overview page
Azure Cache for Redis is retiring

Comment on lines +260 to +264
When the sample app starts, the <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> instance is injected into the _Program.cs_ file. The current time is cached by using the <xref:Microsoft.Extensions.Hosting.IHostApplicationLifetime> interface. (For more information, see [.NET Generic Host: IHostApplicationLifetime](xref:fundamentals/host/generic-host#ihostapplicationlifetime).)

[!code-csharp[](~/performance/caching/distributed/samples/6.x/DistCacheSample/Program.cs?name=snippet_Configure)]

The sample app injects <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> into the `IndexModel` for use by the Index page.
The sample app injects the <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> instance into the `IndexModel` object for use by the Index page.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here and throughout these docs, adding instance and object, etc. where appropriate instead of the API reference by itself.

Comment on lines +294 to +298
* [Cache in-memory in ASP.NET Core](xref:performance/caching/memory)
* [Detect changes with change tokens in ASP.NET Core](xref:fundamentals/change-tokens)
* [Response caching in ASP.NET Core](xref:performance/caching/response)
* [Response caching middleware in ASP.NET Core](xref:performance/caching/middleware)
* [Host ASP.NET Core in a web farm](xref:host-and-deploy/web-farm)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a few links were removed. I would like to keep the ones that people might otherwise not think of looking for and drop those that are nearby in the TOC.

The [RFC 9111: HTTP Caching (Section 5.2. Cache-Control)](https://www.rfc-editor.org/rfc/rfc9111#field.cache-control) specification requires a cache to honor a valid `Cache-Control` header sent by the client. A client can make requests with a `no-cache` header value and force the server to generate a new response for every request.

Always honoring client `Cache-Control` request headers makes sense if you consider the goal of HTTP caching. Under the official specification, caching is meant to reduce the latency and network overhead of satisfying requests across a network of clients, proxies, and servers. It isn't necessarily a way to control the load on an origin server.
Always honoring client `Cache-Control` request headers makes sense if you consider the goal of HTTP caching. Under the official specification, caching is meant to reduce the latency and network overhead of satisfying requests across a network of clients, proxies, and servers. The approach isn't necessarily a way to control the load on an origin server.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"It" here most likely refers back to "caching" in "caching is meant to reduce..."

"The approach" doesn't fit as well with what comes before or what comes after.

@tdykstra tdykstra self-requested a review May 1, 2026 22:05
@tdykstra tdykstra dismissed their stale review May 1, 2026 22:08

The requested change would have taken us out of the scope of light freshness edit. we will save the Redis change for another issue.

Copy link
Copy Markdown
Contributor

@tdykstra tdykstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, will merge when comments are addressed., except ignore the Redis service change.

@GitHubber17
Copy link
Copy Markdown
Contributor Author

@tdykstra - Thank you for your review. I applied your requested changes and held off on changes related to comments about changing the Azure Redis service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants