Now Reading: How to implement caching in ASP.NET Core minimal APIs

Loading
svg

How to implement caching in ASP.NET Core minimal APIs

NewsSeptember 11, 2025Artifice Prime
svg5

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.

  1. Launch the Visual Studio 2022 IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web API” from the list of templates displayed.
  4. Click “Next.”
  5. 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.
  6. Click “Next.”
  7. 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.
  8. 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.
  9. 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.

Original Link:https://www.infoworld.com/article/4053115/how-to-implement-caching-in-asp-net-core-minimal-apis.html
Originally Posted: Thu, 11 Sep 2025 09:00:00 +0000

0 People voted this article. 0 Upvotes - 0 Downvotes.

Artifice Prime

Atifice Prime is an AI enthusiast with over 25 years of experience as a Linux Sys Admin. They have an interest in Artificial Intelligence, its use as a tool to further humankind, as well as its impact on society.

svg
svg

What do you think?

It is nice to know your opinion. Leave a comment.

Leave a reply

Loading
svg To Top
  • 1

    How to implement caching in ASP.NET Core minimal APIs

Quick Navigation