Roadmap › ASP.NET Core MVC (and Razor Pages) › Module: Filters, State and Security
Lessons in this course (15)
14. Caching, session and performance in MVC
IntermediateMediumLesson 14 of 15
csharp
builder.Services.AddOutputCache(o => o.AddPolicy("Products", b => b.Expire(TimeSpan.FromMinutes(5)).Tag("products")));
app.UseOutputCache();
[OutputCache(PolicyName = "Products")]
public IActionResult Index() => View();
// invalidate after an update: await outputCacheStore.EvictByTagAsync("products", ct);cshtml
<cache expires-after="@TimeSpan.FromMinutes(10)" vary-by-user="true">
@await Component.InvokeAsync("RecentOrders")
</cache>Other levers: IMemoryCache/IDistributedCache/HybridCache for data, response compression, bundling/minification (or MapStaticAssets compression), async actions, avoiding N+1 queries in views (load everything in the controller with projections).