Output caching — cache complete HTTP responses.
Setup:
1builder.Services.AddOutputCache();2app.UseOutputCache();
Per-endpoint:
1app.MapGet("/products", async (IDbContext db) =>2 await db.Products.ToListAsync())3 .CacheOutput(TimeSpan.FromMinutes(10));
Cache policies:
1builder.Services.AddOutputCache(options =>2{3 options.AddBasePolicy(builder =>4 builder.Expire(TimeSpan.FromMinutes(5)));56 options.AddPolicy("ProductCache", builder =>7 builder8 .Expire(TimeSpan.FromMinutes(10))9 .Tag("products"));10});
Invalidation:
1app.MapPost("/products", async (...) =>2{3 // Create product...4 await ctx.RequestServices5 .GetRequiredService<OutputCacheInvalidationManager>()6 .InvalidateTagAsync("products");7});
Vary by: query string, header, route value.