Output caching
Output caching middleware was introduced with ASP.NET Core 7 and it can be used in all types of ASP.NET Core projects.
Output caching endpoints
Output caching stores dynamically generated responses on the server so that they do not have to be regenerated again for another request. This can improve performance.
Let's see it in action with examples of applying output caching to some endpoints:
- In the
Northwind.Mvc
project, at the top ofProgram.cs
, import the name for our class of duration constants, as shown in the following code:
using Northwind.Mvc; // To use DurationInSeconds.
- In
Program.cs
, before the call toBuild
, add statements to add the output cache middleware and override the default expiration timespan to make it only 10 seconds, as shown highlighted in the following code:
builder.Services.AddOutputCache(options =>
{
options.DefaultExpirationTimeSpan =
TimeSpan.FromSeconds(DurationInSeconds.TenSeconds);
});
var app = builder.Build();
Good...