Output caching in Asp.net Core 7.0 Preview 6

 In the previous article, we discussed ASP.NET Core and Blazor updates in .NET 7 Preview 6

In this article, we will discuss Output caching in .Net 7 previews 6.

 Output caching is the new middleware in asp.net core for .NET 7 preview 6 which helps us to store results data from our web application and then get data from a caching instead of analyzing them every time, this improves the performance and uses resources for other activities. 

To start working with output caching, need to use the AddOutputCache extension method in IServiceCollection and UseOutputCache extension method in IApplicationBuilder.

First let's create an application in Visual Studio 2022, after creating an application let's go to Program.cs class, you will see the below code in Program.cs class.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOutputCache();

var app = builder.Build();

app.UseOutputCache();

app.MapGet("/", () => "Hello World!");

app.Run();

in the program class, you can see we have builder.Services.AddOutputCache(); and app.UseOutputCache();

This helps us to do output caching.Net core 7.

Let's understand output caching with an example that prints the current date and time

app.MapGet("/Current_time", () => DateTime.Now.ToString());
So in the above code whenever we refresh the browser, it displays the current date and time, now let's add caching to the above code

After doing the above changes then we need to configure the output caching on endpoints like below

app.MapGet("/Current_time", () => DateTime.Now.ToString());
app.MapGet("/Current_time_cached", () => DateTime.Now.ToString())
.CacheOutput();


Now if you go to the browser and refresh the page, it will not change the date and time. so when you land the first time on the page, it caches the date and time, and 2nd time it doesn't refresh it and you get the cached output every time. 

If you provide a more dynamic API that receives parameters using query strings. You can vary the cache by the query string
Requests which are coming from the web are sent to the “/
Current_time_cached” check for the current time. The “/Current_time_cached” endpoint uses the.CacheOutput() extension method, by using it, each request to “/Current_time_cached” after the first one will get cached response. 

Even we can customize the output caching by using VaryByQuery. 

We can control the caching using VaryByQuery by passing parameters as below.

// Cached entries will vary by culture, but any other additional query 
// is ignored and returns the same cached content.
app.MapGet("/query
", () => DateTime.Now.ToString().CacheOutput(p=>p.VaryByQuery("culture")); // VaryByQuery allow us to add more than one query string like below app.MapGet("/query", () => DateTime.Now.ToString()).CacheOutput(p => p.VaryByQuery("time", "culture", "format"));

Now in the above example, if you want to reduce the caching output for a particular time, also we can do like below

 builder.Services.AddOutputCache(options =>
 {
     options.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(20);
 });
As shown in the above code, we can reduce the output cache expiration time, now it will store the output cache for 20 seconds and after that, if we will refresh the page, it will change the date and time.

-------------------------------------------------------------




Share this

Related Posts

Previous
Next Post »

2 comments

Write comments