[API Proposal]: Microsoft.AspNetCore.OutputCaching consider adding a way to change expiration time based on logic in IOutputCachePolicy
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Background and motivation
To dynamically set cache expiration based on customer logic.
### API Proposal
No changes to api
### API Usage
```csharp
ValueTask IOutputCachePolicy.ServeResponseAsync(OutputCacheContext c, CancellationToken ct)
{
switch (c.HttpContext.Response.StatusCode)
{
case >= 200 and < 300:
c.AllowCacheStorage = true;
break;
case >= 400 and < 500 when CachedResponseValidForPropertyInfo != null:
c.AllowCacheStorage = true;
// currently ignored by middleware
c.ResponseExpirationTimeSpan = TimeSpan.FromMinutes(0.5);
// CachedResponseValidForPropertyInfo.SetValue(c, _errorExpirationBoxed);
break;
default:
c.AllowCacheStorage = false;
break;
};
return ValueTask.CompletedTask;
}
```
### Alternative Designs
_No response_
### Risks
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.