Output caching
In some ways, output caching is like response caching, which we covered in Chapter 9, Caching, Queuing, and Resilient Background Services. Output caching can store dynamically generated responses on the server so that they do not have to be regenerated again for another request. This can improve performance. Unlike response caching, output caching does not rely on clients and intermediaries doing what they are told by the HTTP response headers.
Output caching endpoints
Let’s see it in action with a really simple example of applying output caching to some endpoints to make sure it is working properly:
- In the
Northwind.Mvc
project, inProgram.cs
, after the call toAddNorthwindContext
, add a statement to add the output cache middleware and override the default expiration timespan to make it only 10 seconds, as shown in the following code:builder.Services.AddOutputCache(options => { options.DefaultExpirationTimeSpan = TimeSpan.FromSeconds...