Selectively Caching Values Inside HttpResponseMessage with Polly – caching series 3/3

Full source code here.

Want to learn more about Polly? Check out my Pluralsight course on it.

This is the last of three posts on caching with Polly. The first showed how to selectively cache HttpResponseMessages based on the status code of the response.

The second showed how to cache values inside the HttpResponseMessage rather than the whole of the response.

This final post will show how to selectively cache values inside the response based on the status of the response.

If you have read the previous posts the content of this will feel familiar.

Changes to Startup

Like the previous post, add a memory cache, a policy registry and a HttpClientFactory to the service collection.

 1public void ConfigureServices(IServiceCollection services)
 2{
 3    services.AddMemoryCache();
 4    services.AddSingleton<IAsyncCacheProvider, MemoryCacheProvider>();
 5
 6    IPolicyRegistry<string> registry = services.AddPolicyRegistry();
 7
 8    services.AddHttpClient("RemoteServer", client =>
 9    {
10        client.BaseAddress = new Uri("http://localhost:5000/api/");
11        client.DefaultRequestHeaders.Add("Accept", "application/json");
12    });
13
14    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
15}

What’s new is in the Configure method, fist step is to add the registry to the method parameters, DI will take care of this for us.

1public void Configure(IApplicationBuilder app, IHostingEnvironment env,
2    IAsyncCacheProvider cacheProvider, IPolicyRegistry<string> registry)
3{

Next is the cache filter, this one examines the tuple of int and HttpStatusCode and, if the HttpStatusCode is an OK the tuple will be cached for the duration specified. If the respones is not OK the filter will set the cache time to 0 which is interpreted as do not cache by the cache policy.

1Func<Context, (int value, HttpStatusCode StatusCode), Ttl> cacheOnly200OkFilter =
2	(context, result) => new Ttl(
3		timeSpan: result.StatusCode == HttpStatusCode.OK ? TimeSpan.FromSeconds(5) : TimeSpan.Zero,
4		slidingExpiration: true
5	);

And this is the cache policy that uses the filter above to store the tuple.

1IAsyncPolicy<(int, HttpStatusCode)> cachePolicy =
2	Policy.CacheAsync<(int, HttpStatusCode)>(
3		cacheProvider.AsyncFor<(int, HttpStatusCode)>(), 
4		new ResultTtl<(int, HttpStatusCode)>(cacheOnly200OkFilter),
5		onCacheError: null);

Lastly, add the policy to the registry.

registry.Add("cacheOnly200OkFilter", cachePolicy);

Changes to the Controller

Over in the CatalogController there many changes.

At the top I take in the HttpClientFactory and policy registry.

1private readonly IHttpClientFactory _httpClientFactory;
2private readonly IPolicyRegistry<string> _policyRegistry;
3
4public CatalogController(IHttpClientFactory httpClientFactory, IPolicyRegistry<string> policyRegistry)
5{
6	_httpClientFactory = httpClientFactory;
7	_policyRegistry = policyRegistry;
8}

Inside the Get method, I setup grab the HttpClient from the HttpClientFactory, set the endpoint of the remote service, take the cache policy from the registry and setup the context for the cache policy to cache based on the incoming request. All of this is similar to normal usage of the Polly cache.

1[HttpGet("{id}")]
2public async Task<IActionResult> Get(int id)
3{
4    string requestEndpoint = $"inventory/{id}";
5    var httpClient = _httpClientFactory.CreateClient("RemoteServer");
6
7    var cachePolicy = _policyRegistry.Get<CachePolicy<(int, HttpStatusCode)>>("cacheOnly200Ok");
8    Context policyExecutionContext = new Context($"GetInventoryById-{id}");	

As in the previous post I don’t call the HttpClient from inside the cache policy, instead I need to call something that returns the int and the HttpStatusCode. I use a local function that makes the request to the remote service, checks the response, deserializes the value and returns a tuple.

 1(int ItemsInStock, HttpStatusCode StatusCode) itemsInStockAndStatusCode =
 2    await cachePolicy.ExecuteAsync(context => MakeRequest(), policyExecutionContext);
 3
 4// local function
 5async Task<(int count, HttpStatusCode statusCode)> MakeRequest()
 6{
 7    HttpResponseMessage response = await httpClient.GetAsync(requestEndpoint);
 8
 9    if (response.IsSuccessStatusCode)
10    {
11        int itemsInStock = await response.Content.ReadAsAsync<int>();
12        return (itemsInStock, response.StatusCode);
13    }
14
15    return (0, response.StatusCode);
16}

When the cachePolicy is executed it first checks if there is value already in the cache, if there is, that will be returned and the local function will not be called. If there is no value in the cache, the local function is called, if the response of the local function is in the 200 range, the value is stored in the cache.

At this point, the action method has not returned anything to its caller.

Below the local function I check the tuple returned from the call to the cache policy and return a response.

1if (itemsInStockAndStatusCode.StatusCode != HttpStatusCode.OK)
2{
3     return StatusCode((int)itemsInStockAndStatusCode.StatusCode);
4}
5return Ok(itemsInStockAndStatusCode.ItemsInStock);

For clarity, here is the full Get method.

 1[HttpGet("{id}")]
 2public async Task<IActionResult> Get(int id)
 3{
 4    string requestEndpoint = $"inventory/{id}";
 5    var httpClient = _httpClientFactory.CreateClient("RemoteServer");
 6
 7    var cachePolicy = _policyRegistry.Get<CachePolicy<(int, HttpStatusCode)>>("cacheOnly200Ok");
 8    Context policyExecutionContext = new Context($"GetInventoryById-{id}");
 9
10    (int ItemsInStock, HttpStatusCode StatusCode) itemsInStockAndStatusCode =
11        await cachePolicy.ExecuteAsync(context => MakeRequest(), policyExecutionContext);
12
13    async Task<(int count, HttpStatusCode statusCode)> MakeRequest()
14    {
15        HttpResponseMessage response = await httpClient.GetAsync(requestEndpoint);
16
17        if (response.IsSuccessStatusCode)
18        {
19            int itemsInStock = JsonConvert.DeserializeObject<int>(await response.Content.ReadAsSt
20            return (itemsInStock, response.StatusCode);
21        }
22
23        // it doesn't matter what int you use here, it won't be cached as the StatusCode is not a
24        return (0, response.StatusCode);
25    }
26
27    if (itemsInStockAndStatusCode.StatusCode != HttpStatusCode.OK)
28    {
29         return StatusCode((int)itemsInStockAndStatusCode.StatusCode);
30    }
31    return Ok(itemsInStockAndStatusCode.ItemsInStock);
32}

Here endeth the series on caching.  
Full source code here.

comments powered by Disqus

Related