@@ -63,24 +63,24 @@ public class Startup
6363 public void ConfigureServices (IServiceCollection services )
6464 {
6565 services .AddMemoryCache ();
66- services .AddSingleton <Polly .Registry .IPolicyRegistry <string >, Polly .Registry .PolicyRegistry >();
67- services .AddSingleton <Polly .Caching .ISyncCacheProvider , Polly .Caching .MemoryCache .MemoryCacheProvider >(); // Or: IAsyncCacheProvider
68- // ...
69- }
66+ services .AddSingleton <Polly .Caching .IAsyncCacheProvider , Polly .Caching .Memory .MemoryCacheProvider >();
67+
68+ services .AddSingleton <Polly .Registry .IPolicyRegistry <string >, Polly .Registry .PolicyRegistry >((serviceProvider ) =>
69+ {
70+ PolicyRegistry registry = new PolicyRegistry ();
71+ registry .Add (" myCachePolicy" , Policy .CacheAsync <HttpResponseMessage >(serviceProvider .GetRequiredService <IAsyncCacheProvider >(), TimeSpan .FromMinutes (5 )));
72+ return registry ;
73+ });
7074
71- public void Configure (..., IPolicyRegistry <string > policyRegistry , ISyncCacheProvider cacheProvider )
72- {
73- var cachePolicy = Policy .Cache (cacheProvider , TimeSpan .FromMinutes (5 )); // Or: Policy.CacheAsync(IAsyncCacheProvider, ...)
74- policyRegistry .Add (" myCachePolicy" , cachePolicy );
7575 // ...
7676 }
7777}
7878
7979// In a controller, inject the policyRegistry and retrieve the policy:
80- // (magic string "myCachePolicy" only hard-coded here to keep the example simple! )
80+ // (magic string "myCachePolicy" only hard-coded here to keep the example simple)
8181public MyController (IPolicyRegistry < string > policyRegistry )
8282{
83- var _cachePolicy = policyRegistry .Get <ISyncPolicy > (" myCachePolicy" ); // Or: IAsyncPolicy
83+ var _cachePolicy = policyRegistry .Get <IAsyncPolicy < HttpResponseMessage >> (" myCachePolicy" );
8484 // ...
8585 }
8686
0 commit comments