Learn how to use in-memory caching, distributed caching, hybrid caching, response caching, or output caching in ASP.NET Core to boost the performance and scalability of your minimal API applications.
When working with ASP.NET Core applications, there are several ways in which you can enhance your applicationโs performance. Caching is one of the most widely used and proven strategies that can significantly boost your applicationโs scalability and performance.
In this post, weโll examine how we can work with caching in minimal APIs in ASP.NET Core. ASP.NET Core offers the flexibility to cache server responses on the client (response caching) or on the server (output caching). In addition, you can choose to cache the data in the memory of the application server (in-memory caching), or in an external data store such as Redis or SQL Server (distributed caching), or a combination of both (hybrid caching). Weโll examine all of these options here.
To use the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you donโt already have a copy, you can download Visual Studio 2022 here.
Create an ASP.NET Core Web API project in Visual Studio 2022
To create an ASP.NET Core Web API project in Visual Studio 2022, follow the steps outlined below.
- Launch the Visual Studio 2022 IDE.
- Click on โCreate new project.โ
- In the โCreate new projectโ window, select โASP.NET Core Web APIโ from the list of templates displayed.
- Click โNext.โ
- In the โConfigure your new projectโ window, specify the name and location for the new project. Optionally check the โPlace solution and project in the same directoryโ check box, depending on your preferences.
- Click โNext.โ
- In the โAdditional Informationโ window shown next, select โ.NET 9.0 (Standard Term Support)โ as the framework version and uncheck the check box that says โUse controllers,โ as weโll be using minimal APIs in this project.
- Elsewhere in the โAdditional Informationโ window, leave the โAuthentication Typeโ set to โNoneโ (the default) and make sure the check boxes โEnable Open API Support,โ โConfigure for HTTPS,โ and โEnable Dockerโ remain unchecked. We wonโt be using any of those features here.
- Click โCreate.โ
Weโll use this ASP.NET Core Web API project to work with the code examples given in the sections below.
Caching in ASP.NET Core
ASP.NET Core provides support for several types of caching. In-memory caching uses the memory of a single server to store cached data. Distributed caching shares cached data across a group of servers. Hybrid caching combines the speed of in-memory caching and the durability of distributed caching. Finally, while response caching enables caching of server responses based on HTTP headers, output caching offers more flexibility in caching server responses. Weโll examine each of these caching methods below.
In-memory caching in minimal APIs
ASP.NET Core provides support for two abstractions for working with caching, IMemoryCache and IDistributedCache. While the former is used to implement in-memory caching, the latter is used to implement distributed caching.
The following use of IMemoryCache shows how you can retrieve data from the cache if the requested data is available. If the data requested is not present in the in-memory cache, the application will retrieve the data from the data store (using a repository), store the data in the in-memory cache, and return it.
app.MapGet("authors/getall", (IMemoryCache cache,
IAuthorRepository authorRepository) =>
{
if (!cache.TryGetValue("get-authors",
out List authors))
{
authors = authorRepository.GetAll();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(5))
.SetSlidingExpiration(TimeSpan.FromMinutes(1));
cache.Set("get-authors", authors, cacheEntryOptions);
}
return Results.Ok(authors);
});
As you can see in the preceding code snippet, the cached content will reside in the memory for a maximum of 30 seconds.
Distributed caching in minimal APIs
Distributed caching enhances the performance and scalability of applications by distributing the load across multiple nodes or servers. The servers can be located either in the same network or in different networks that are spread across geographical distances.
The following code demonstrates how to implement distributed caching in a minimal API endpoint in ASP.NET Core. In this example, the endpoint returns all author records from the distributed cache if the data is available in the cache. If the requested data is not available in the distributed cache, the endpoint adds the data to the cache and then returns the data.
app.MapGet("/getallauthors", async (IDistributedCache cache) =>
{
var cacheKey = "get-all-authors";
var cachedMessage = await cache.GetStringAsync(cacheKey);
if (cachedMessage == null)
{
cachedMessage = $"The data has been cached at {DateTime.Now}";
await cache.SetStringAsync(cacheKey, cachedMessage, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60)
});
}
return Results.Ok(cachedMessage);
});
Hybrid caching in minimal APIs
Starting from .NET 9, you can take advantage of hybrid caching in your ASP.NET Core applications. The HybridCache API, as the name suggests, blends the capabilities of both in-memory caching and distributed caching, thereby addressing the shortcomings of each.
The following code snippet shows how you can configure hybrid caching in the Program.cs file of your ASP.NET Core application.
services.AddHybridCache(options => {
options.DefaultEntryOptions = new HybridCacheEntryOptions
{
Expiration = TimeSpan.FromMinutes(5),
LocalCacheExpiration = TimeSpan.FromMinutes(5)
};
});
Response caching in minimal APIs
Response caching uses cache-related HTTP headers to cache server responses. Response caching reduces the number of requests made to the web server, thereby reducing latency and improving application scalability. You can implement response caching in ASP.NET Core in two ways. You can use the[ResponseCache] attribute to enable response caching on the client side, or you can use the Response Caching Middleware to enable response caching on the server.
The line of code below shows how you can add the Response Caching Middleware to the services collection in ASP.NET Core.
builder.Services.AddResponseCaching();
The following line of code shows how you can add the Response Caching Middleware to the request processing pipeline.
app.UseResponseCaching();
Output caching in minimal APIs
With output caching, the output of a request is cached so that all subsequent requests can return data from the cache. Output caching is implemented in ASP.NET Core by calling CacheOutput or by applying the [OutputCache] attribute.
Output caching differs from response caching in several ways. Most importantly, whereas response caching is based on HTTP headers, output caching is configured on the server. This means that you can invalidate cache entries programmatically and that clients canโt override your desired caching behavior.
The following code snippet shows how you can implement output caching for a minimal API endpoint in ASP.NET Core.
app.MapPost("/author/getauthors", ([FromServices] IAuthorRepository authorRepository) =>
{
return authorRepository.GetAll();
}).CacheOutput(x => x.Expire(TimeSpan.FromSeconds(30)));
Note that, while response caching is limited to memory, output caching allows you to configure your cache storage. Hence, whereas you can use response caching only with in-memory caching, you can use output caching with in-memory, distributed, or hybrid caching.
Caching best practices in ASP.NET Core minimal APIs
The following are the key practices you should follow to make the best use of caching in your ASP.NET Core applications:
- You should choose the right caching strategy, i.e., use in-memory cache for applications that handle fewer amount of data, distributed cache if the application is resource intensive and needs to scale in a distributed environment.
- You should set proper expiration policies per your applicationโs requirements.
- You should not cache sensitive data.
- You should use cache invalidation whenever it is appropriate.
- You should keep an eye on cache hit/miss ratios to understand how your caching strategy is working in real-time.
Besides using the right caching strategy (i.e., in-memory, distributed, or hybrid), based on your applicationโs requirements, you should also use an appropriate cache expiration strategy to have better control over cache lifetimes. A cache lifetime denotes the amount of time a cached object would remain in the cache. Iโll discuss this further in another post soon.


