Skip to content

Commit f58c673

Browse files
committed
General readme improvements
1 parent e2ccd61 commit f58c673

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

README.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,53 +44,51 @@ Polly.Caching.MemoryCache v1.* requires:
4444

4545
# How to use the Polly.Caching.Memory plugin
4646

47-
```csharp
48-
// (1a): Create a MemoryCacheProvider instance in the .NET Framework, using the Polly.Caching.Memory nuget package.
49-
// (full namespaces and types only shown here for disambiguation)
50-
Polly.Caching.Memory.MemoryCacheProvider memoryCacheProvider
51-
= new Polly.Caching.Memory.MemoryCacheProvider(System.Runtime.Caching.MemoryCache.Default);
47+
### Example: Direct creation of CachePolicy (no DI)
5248

53-
// Or (1b): Create a MemoryCacheProvider instance in .NET Core / .NET Standard.
54-
// (full namespaces and types only shown here for disambiguation)
55-
// NB Only if you want to create your own Microsoft.Extensions.Caching.Memory.MemoryCache instance:
49+
```csharp
50+
// This approach creates a CachePolicy directly, with its own Microsoft.Extensions.Caching.Memory.MemoryCache instance:
5651
Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache
5752
= new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
5853
Polly.Caching.Memory.MemoryCacheProvider memoryCacheProvider
5954
= new Polly.Caching.Memory.MemoryCacheProvider(memoryCache);
6055

61-
// (2) Create a Polly cache policy using that Polly.Caching.Memory.MemoryCacheProvider instance.
56+
// Create a Polly cache policy using that Polly.Caching.Memory.MemoryCacheProvider instance.
6257
var cachePolicy = Policy.Cache(memoryCacheProvider, TimeSpan.FromMinutes(5));
58+
```
6359

60+
### Example: Configure CachePolicy via MemoryCacheProvider in StartUp, for DI
6461

62+
```csharp
63+
// (We pass a whole PolicyRegistry by dependency injection rather than the individual policy,
64+
// on the assumption the app will probably use multiple policies.)
6565
66-
// Or (1c): Configure by dependency injection within ASP.NET Core
67-
// See https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory
68-
// and https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection#registering-your-own-services
69-
70-
// (In this example we choose to pass a whole PolicyRegistry by dependency injection rather than the individual policy, on the assumption the webapp will probably use multiple policies across the app.)
71-
72-
// For example:
7366
public class Startup
7467
{
7568
public void ConfigureServices(IServiceCollection services)
7669
{
7770
services.AddMemoryCache();
7871
services.AddSingleton<Polly.Caching.IAsyncCacheProvider, Polly.Caching.Memory.MemoryCacheProvider>();
7972

80-
services.AddSingleton<Polly.Registry.IPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
73+
services.AddSingleton<Polly.Registry.IReadOnlyPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
8174
{
8275
PolicyRegistry registry = new PolicyRegistry();
83-
registry.Add("myCachePolicy", Policy.CacheAsync<HttpResponseMessage>(serviceProvider.GetRequiredService<IAsyncCacheProvider>().AsyncFor<HttpResponseMessage>(), TimeSpan.FromMinutes(5)));
76+
registry.Add("myCachePolicy",
77+
Policy.CacheAsync<HttpResponseMessage>(
78+
serviceProvider
79+
.GetRequiredService<IAsyncCacheProvider>()
80+
.AsyncFor<HttpResponseMessage>(),
81+
TimeSpan.FromMinutes(5)));
8482
return registry;
8583
});
8684

8785
// ...
8886
}
8987
}
9088

91-
// In a controller, inject the policyRegistry and retrieve the policy:
89+
// At the point of use, inject the policyRegistry and retrieve the policy:
9290
// (magic string "myCachePolicy" only hard-coded here to keep the example simple)
93-
public MyController(IPolicyRegistry<string> policyRegistry)
91+
public MyController(IReadOnlyPolicyRegistry<string> policyRegistry)
9492
{
9593
var _cachePolicy = policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("myCachePolicy");
9694
// ...

0 commit comments

Comments
 (0)