In a .NET 8 minimal API I've implement output-caching on one endpoint (in a Carter module).
const int cacheDurationInMinutes = 10;
var clientGroup = app.MapGroup("/api/v{version:apiVersion}/client")
.WithApiVersionSet(_apiVersionSet.Value)
.WithTags("Client");
clientGroup.MapGet("/", GetClient)
.WithName(nameof(GetClient))
.WithOpenApi(operation =>
{
operation.Summary = "Obtains the current configured client.";
return operation;
})
.CacheOutput(policyBuilder => policyBuilder.Expire(TimeSpan.FromMinutes(cacheDurationInMinutes))
.Tag("tag-client"));
You can see here that I've set the cache duration to be 10 minutes, and I've declare the tag tag-client
on the cache policy.
If I add a breakpoint at the start of this method, then debug from a .http file, it hits the breakpoint on the first call. If I send another request it doesn't hit the breakpoint - good, caching is being implemented.
In Program.cs
I have...
app.MapPost("/purge/{tag}", async (IOutputCacheStore cache, string tag) =>
{
await cache.EvictByTagAsync(tag, default);
});
I then send a call to /purge/tag-client
: it hits that ^^ anonymous function and then I receive a 200 (OK
) response.
If I then send another request to the method which implements caching I still doesn't hit the breakpoint - presumably because there's a cached result?
Why is there still a cached result after I've purged it?