@@ -486,7 +486,7 @@ The generated OpenAPI document will include the correct media type and response
486486
487487## `IOutputCachePolicyProvider` for custom output caching
488488
489- 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 .
489+ 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 , databases , or implementing custom policy resolution logic based on tenant - specific settings .
490490
491491```csharp
492492public interface IOutputCachePolicyProvider
@@ -500,47 +500,61 @@ public interface IOutputCachePolicyProvider
500500** Usage : **
501501
502502```csharp
503- public class CustomOutputCachePolicyProvider : IOutputCachePolicyProvider
503+ public class DatabaseOutputCachePolicyProvider : IOutputCachePolicyProvider
504504{
505- private readonly IConfiguration _configuration ;
505+ private readonly IOptions < OutputCacheOptions > _options ;
506+ private readonly IPolicyDatabase _policyDatabase ;
506507
507- public CustomOutputCachePolicyProvider (IConfiguration configuration )
508+ public DatabaseOutputCachePolicyProvider (
509+ IOptions < OutputCacheOptions > options ,
510+ IPolicyDatabase policyDatabase )
508511 {
509- _configuration = configuration ;
512+ _options = options ;
513+ _policyDatabase = policyDatabase ;
510514 }
511515
512516 public IReadOnlyList < IOutputCachePolicy > GetBasePolicies ()
513517 {
514- // Return base policies that apply to all requests
515- return new List < IOutputCachePolicy >
518+ // Return base policies from options
519+ if ( _options . Value . BasePolicies is not null && _options . Value . BasePolicies . Count > 0 )
516520 {
517- new OutputCachePolicyBuilder ().Expire (TimeSpan .FromMinutes (5 )).Build ()
518- };
521+ return _options .Value .BasePolicies ;
522+ }
523+ return Array .Empty <IOutputCachePolicy >();
519524 }
520525
521- public ValueTask < IOutputCachePolicy ? > GetPolicyAsync (string policyName )
526+ public async ValueTask < IOutputCachePolicy ? > GetPolicyAsync (string policyName )
522527 {
523- // Custom logic to resolve named policies
524- // For example, load from external configuration
525- var duration = _configuration .GetValue <int >($" CachePolicies:{policyName }:DurationMinutes" );
526-
527- if (duration > 0 )
528+ // First check the configured options
529+ if (_options .Value .NamedPolicies ? .TryGetValue (policyName , out var policy ) == true )
528530 {
529- var policy = new OutputCachePolicyBuilder ()
530- .Expire (TimeSpan .FromMinutes (duration ))
531- .Build ();
532- return ValueTask .FromResult <IOutputCachePolicy ?>(policy );
531+ return policy ;
533532 }
534533
535- return ValueTask .FromResult <IOutputCachePolicy ?>(null );
534+ // Fall back to loading from database
535+ var dbPolicy = await _policyDatabase .GetPolicyAsync (policyName );
536+ return dbPolicy ;
536537 }
537538}
538539
539540// Registration
540- services .AddSingleton <IOutputCachePolicyProvider , CustomOutputCachePolicyProvider >();
541+ builder .Services .AddOutputCache (options =>
542+ {
543+ // Configure default policies using OutputCacheOptions
544+ options .AddBasePolicy (builder => builder .Expire (TimeSpan .FromMinutes (5 )));
545+ options .AddPolicy (" short" , builder => builder .Expire (TimeSpan .FromMinutes (1 )));
546+ });
547+
548+ // Replace the default provider with custom implementation
549+ builder .Services .AddSingleton <IOutputCachePolicyProvider , DatabaseOutputCachePolicyProvider >();
541550```
542551
543- 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 .
552+ This interface provides extensibility for output caching , enabling scenarios like :
553+
554+ - Loading policies from external configuration stores
555+ - Implementing tenant - specific caching strategies
556+ - Dynamic policy resolution based on user roles or permissions
557+ - Integrating with existing policy management systems
544558
545559Thank you [@lqlive ](https :// github.com/lqlive) for this contribution!
546560
0 commit comments