Response Caching
In order to get response caching working, you need to enable the response caching middleware and define how responses are cached using the ResponseCache() method in the endpoint configuration.
This method supports all arguments of the [ResponseCache] attribute you'd typically use with mvc except for the CacheProfileName argument as cache profiles are not supported.
See this document for an intro to response caching in ASP.NET middleware.
Program.cs
global using FastEndpoints;
var builder = WebApplication.CreateBuilder();
builder.Services.AddFastEndpoints();
builder.Services.AddResponseCaching(); //add this
var app = builder.Build();
app.UseAuthorization();
app.UseResponseCaching(); //add this
app.UseFastEndpoints();
app.Run();
MyEndpoint.cs
public class MyEndpoint : EndpointWithoutRequest
{
public override void Configure()
{
Get("/api/cached-ticks");
ResponseCache(60); //cache for 60 seconds
}
public override Task HandleAsync(CancellationToken ct)
{
return SendAsync(new
{
Message = "this response is cached"
Ticks = DateTime.UtcNow.Ticks
});
}
}
TIP
Output caching features introduced in .NET 7.0 can be added to endpoints via the CacheOutput(...) extension method.
public override void Configure()
{
...
Options(x => x.CacheOutput(p => p.Expire(TimeSpan.FromSeconds(60))));
}