Skip to content

Commit 33b944b

Browse files
Copilotdanroth27
andcommitted
Fix IOutputCachePolicyProvider interface to match actual implementation
Co-authored-by: danroth27 <1874516+danroth27@users.noreply.github.com>
1 parent 6c7bd56 commit 33b944b

1 file changed

Lines changed: 35 additions & 12 deletions

File tree

release-notes/11.0/preview/preview1/aspnetcore.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,14 @@ The generated OpenAPI document will include the correct media type and response
479479

480480
## `IOutputCachePolicyProvider` for custom output caching
481481

482-
ASP.NET Core now provides `IOutputCachePolicyProvider` for implementing custom output caching policy selection logic. This interface enables advanced scenarios where caching policies need to be determined dynamically based on request context or other runtime factors.
482+
ASP.NET Core now provides `IOutputCachePolicyProvider` for implementing custom output caching policy selection logic. This interface enables advanced scenarios where caching policies need to be determined dynamically, such as retrieving policies from external configuration sources or implementing custom policy resolution logic.
483483

484484
```csharp
485485
public interface IOutputCachePolicyProvider
486486
{
487-
ValueTask<IOutputCachePolicy?> GetPolicyAsync(
488-
HttpContext context,
489-
string? policyName);
487+
IReadOnlyList<IOutputCachePolicy> GetBasePolicies();
488+
489+
ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName);
490490
}
491491
```
492492

@@ -495,24 +495,47 @@ public interface IOutputCachePolicyProvider
495495
```csharp
496496
public class CustomOutputCachePolicyProvider : IOutputCachePolicyProvider
497497
{
498-
public ValueTask<IOutputCachePolicy?> GetPolicyAsync(
499-
HttpContext context,
500-
string? policyName)
498+
private readonly IConfiguration _configuration;
499+
500+
public CustomOutputCachePolicyProvider(IConfiguration configuration)
501+
{
502+
_configuration = configuration;
503+
}
504+
505+
public IReadOnlyList<IOutputCachePolicy> GetBasePolicies()
501506
{
502-
// Custom logic to select caching policy
503-
if (context.User.IsInRole("Premium"))
507+
// Return base policies that apply to all requests
508+
return new List<IOutputCachePolicy>
504509
{
505-
return new ValueTask<IOutputCachePolicy?>(premiumPolicy);
510+
new OutputCachePolicyBuilder().Expire(TimeSpan.FromMinutes(5)).Build()
511+
};
512+
}
513+
514+
public ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName)
515+
{
516+
// Custom logic to resolve named policies
517+
// For example, load from external configuration
518+
var duration = _configuration.GetValue<int>($"CachePolicies:{policyName}:DurationMinutes");
519+
520+
if (duration > 0)
521+
{
522+
var policy = new OutputCachePolicyBuilder()
523+
.Expire(TimeSpan.FromMinutes(duration))
524+
.Build();
525+
return ValueTask.FromResult<IOutputCachePolicy?>(policy);
506526
}
507-
return new ValueTask<IOutputCachePolicy?>(standardPolicy);
527+
528+
return ValueTask.FromResult<IOutputCachePolicy?>(null);
508529
}
509530
}
510531

511532
// Registration
512533
services.AddSingleton<IOutputCachePolicyProvider, CustomOutputCachePolicyProvider>();
513534
```
514535

515-
This interface provides extensibility for output caching and enables integration with custom policy management systems.
536+
This interface provides extensibility for output caching and enables scenarios like loading policies from external configuration sources or implementing custom policy resolution based on tenant-specific settings.
537+
538+
Thank you [@lqlive](https://github.com/lqlive) for this contribution!
516539
517540
## `TimeProvider` in ASP.NET Core Identity
518541

0 commit comments

Comments
 (0)