Skip to content

Commit 8f8407b

Browse files
committed
Update Readme with serialization example
1 parent e26539c commit 8f8407b

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var cachePolicy = Policy.CacheAsync<byte[]>(distributedCache.AsAsyncCacheProvide
7272

7373
```
7474

75-
Configuration via DI in ASPNET Core:
75+
## Configuration via DI in ASPNET Core:
7676

7777
```csharp
7878
// 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.
@@ -111,7 +111,65 @@ public MyController(IReadOnlyPolicyRegistry<string> policyRegistry)
111111
}
112112
```
113113

114-
Usage:
114+
## Automatically serializing more complex type
115+
116+
The raw cache provider `Polly.Caching.IDistributedCache` allows you to cache items of type `byte[]` or `string` as those are the native formats supported by [`Microsoft.Extensions.Caching.Distributed.IDistributedCache`](https://docs.microsoft.com/en-gb/dotnet/api/microsoft.extensions.caching.distributed.idistributedcache). However, Polly also allows you to automatically serialize more complex types.
117+
118+
The package `Polly.Caching.Serialization.Json` ([github](https://github.com/App-vNext/Polly.Caching.Serialization.Json); [nuget](https://www.nuget.org/packages/Polly.Caching.Serialization.Json)) is a Polly [`ICacheItemSerializer<TResult, string>`](https://github.com/App-vNext/Polly/wiki/Implementing-cache-serializers#using-a-serializer-with-the-polly-cachepolicy) to serialize any type for use with `Polly.Caching.IDistributedCache`.
119+
120+
Configuration in .NET Core:
121+
122+
```csharp
123+
public class Startup
124+
{
125+
public void ConfigureServices(IServiceCollection services)
126+
{
127+
services.AddDistributedRedisCache(options =>
128+
{
129+
options.Configuration = "localhost"; // or whatever
130+
options.InstanceName = "SampleInstance";
131+
});
132+
133+
// Obtain a Newtonsoft.Json.JsonSerializerSettings defining any settings to use for serialization
134+
// (could alternatively be obtained from a factory by DI)
135+
var serializerSettings = new JsonSerializerSettings()
136+
{
137+
// Any configuration options
138+
};
139+
140+
// Register a Polly cache provider for caching ProductDetails entities, using the IDistributedCache instance and a Polly.Caching.Serialization.Json.JsonSerializer.
141+
// (ICacheItemSerializer<ProductDetails, string> could alternatively be obtained from a factory by DI)
142+
services.AddSingleton<Polly.Caching.IAsyncCacheProvider<ProductDetails>>(serviceProvider =>
143+
serviceProvider
144+
.GetRequiredService<IDistributedCache>()
145+
.AsAsyncCacheProvider<string>()
146+
.WithSerializer<ProductDetails, string>(
147+
new Polly.Caching.Serialization.Json.JsonSerializer<ProductDetails>(serializerSettings)
148+
);
149+
150+
// Register a Polly cache policy for caching ProductDetails entities, using that IDistributedCache instance.
151+
services.AddSingleton<Polly.Registry.IReadOnlyPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
152+
{
153+
PolicyRegistry registry = new PolicyRegistry();
154+
registry.Add("productsCachePolicy", Policy.CacheAsync<ProductDetails>(serviceProvider.GetRequiredService<IAsyncCacheProvider<ProductDetails>>(), TimeSpan.FromMinutes(5)));
155+
156+
return registry;
157+
});
158+
159+
// ...
160+
}
161+
}
162+
163+
// In a controller, inject the policyRegistry and retrieve the policy:
164+
// (magic string "productsCachePolicy" hard-coded here only to keep the example simple)
165+
public MyController(IReadOnlyPolicyRegistry<string> policyRegistry)
166+
{
167+
var _cachePolicy = policyRegistry.Get<IAsyncPolicy<ProductDetails>>("productsCachePolicy");
168+
// ...
169+
}
170+
```
171+
172+
## Usage at the point of consumption
115173

116174
```csharp
117175
string productId = // ... from somewhere

0 commit comments

Comments
 (0)