Caching of output in ASP.NET Core

Output caching is a great strategy for improving the performance of your web applications. In this blog article, we will define output caching, explain how it works in ASP.NET Core, and show you how to use it in your projects.

What exactly is Output Caching?

The process of keeping the response to a web request in memory or on disk and reusing it for later queries that meet particular criteria is known as output caching. The goal of output caching is to reduce the amount of work required by the web server for each request, such as executing code, searching databases, or generating views. The web server can handle more requests faster and with less resources by serving cached responses.

There are various types of output caching based on where the response is stored and who has access to it. The most prevalent types are as follows:

  • Client-side caching occurs when the response is cached in the browser and reused by the same client. This sort of caching is governed by HTTP headers such as Cache-Control and Expires, which specify how long and under what conditions the cached answer can be used by the browser.
  • Proxy caching: The response is cached and reused by numerous clients by storing it in an intermediary proxy server (such as a CDN). HTTP headers, such as ETag, which specify how the proxy can validate and vary the cached answer, also regulate this type of caching.
  • Server-side caching: The response is cached in the memory or disk of the web server and reused by multiple clients.
  • This sort of caching is managed by the logic of the web application, which determines which replies to cache and how to invalidate them.

Caching of output in ASP.NET Core

ASP.NET Core is a modern web framework that includes output caching by default. Output Caching Middleware, which is included in ASP.NET Core 7.0 and later, caches replies rather than HTTP headers. It functions similarly to a server-side cache and can help both UI apps and API requests. You can specify what replies to cache, how long to cache them for, where to keep them, and how to expire them with output caching. Cache entry dependencies, storage media extensibility, cache profiles, and other features are also supported by output caching.

builder.Services.AddOutputCache(options =>
    // Add a base policy that applies to all endpoints
    options.AddBasePolicy(basePolicy => basePolicy.Expire(TimeSpan.FromSeconds(120)));

    // Add a named policy that applies to selected endpoints
    options.AddPolicy("Expire20", policyBuilder => policyBuilder.Expire(TimeSpan.FromSeconds(20)));

and then add the below code to use output caching.


Using attributes for action-level output caching

You need to apply the [OutputCache] attribute to your endpoints (such as actions or pages) that you want to enable output caching for. You can use the attribute without parameters to apply the base policy or with a policy name or inline settings to apply a custom policy. For example.

// Use the base policy
public IActionResult Index()
    return View();

// Use a named policy
public IActionResult About()
    return View();

// Use inline settings
[OutputCache(Duration = 30)]
public IActionResult Contact()
    return View();

Output caching is an important technique that can help you improve the performance of your web applications by reducing the load on your web server and delivering faster responses to your clients. ASP.NET Core provides a built-in output caching middleware that allows you to configure and control output caching with ease and flexibility.

In this blog post, we have learned what output caching is, how it works in ASP.NET Core, and how to implement it in your projects.

Happy coding!

Best and Most Recommended ASP.NET Core 8 Hosting

Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 8 web ranking. HostForLIFEASP.NET is highly recommended. In Europe, HostForLIFEASP.NET is the most popular option for first-time web hosts searching for an affordable plan.

Their standard price begins at only € 3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFEASP.NET guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.

By providing reseller hosting accounts, HostForLIFEASP.NET also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.