dotnet / dotnet/aspnetcore

[API Proposal]: Microsoft.AspNetCore.OutputCaching consider adding a way to change expiration time based on logic in IOutputCachePolicy

Open
#52,670 3 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-middleware feature-output-caching
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.