question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example with Polly.CacheAsync with ResultTtl strategy needed

See original GitHub issue

I’ve tried to create a CachePolicy using the ResultTtl strategy. It happens that I have to use IAsyncCacheProvider<TResult> to make it possible, at least against Polly 5.9.0

Unfortunatly I cannot find out how to create such provider. My best guess was to use IDistributedCache provider and a serializer but currently the package doesn’t look present on nuget https://www.nuget.org/packages/polly.caching.serialization.json

Could someone guide me further on this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
AndreaCuneocommented, May 29, 2018

I tried the following code with polly 5.9.0 but I get the following error compiling.

#if NETSTANDARD2_0
        Polly.Caching.MemoryCache.MemoryCacheProvider _memoryCacheProvider
           = new Polly.Caching.MemoryCache.MemoryCacheProvider(new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()));
#else 
        Polly.Caching.MemoryCache.MemoryCacheProvider _memoryCacheProvider
           = new Polly.Caching.MemoryCache.MemoryCacheProvider(System.Runtime.Caching.MemoryCache.Default);
#endif

var cachePolicy = Policy.CacheAsync<(string AccessToken, DateTimeOffset ExpiresOn)>(_memoryCacheProvider, new ResultTtl<(string AccessToken, DateTimeOffset ExpiresOn)>(r => new Ttl(r.ExpiresOn - DateTimeOffset.Now, false)));

Argument 2: cannot convert from ‘Polly.Caching.ResultTtl<(string AccessToken, System.DateTimeOffset ExpiresOn)>’ to ‘System.TimeSpan’

And makes sense as there is no overload that accept ITtlStrategy<TResult> and a plain IAsyncCacheProvider. It requires IAsyncCacheProvider<TResult> but I do not find how to make one. If I update to Polly v6, there is a version clashing where Polly.Caching.MemoryCache.MemoryCacheProvider doesn’t implement Polly.Caching.IAsyncCacheProvider from v6 and requires v5.9

Am I doing something wrong?

0reactions
reisenbergercommented, Sep 8, 2018

I confirm that AsyncFor<> solves. I couldn’t find that, or I missed it completly, from every doc page I looked at

Documentation now added: https://github.com/App-vNext/Polly.Caching.MemoryCache#note . Thanks for highlighting this issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Example with Polly.CacheAsync with ResultTtl strategy ...
I've tried to create a CachePolicy using the ResultTtl strategy. It happens that I have to use IAsyncCacheProvider<TResult> to make it ...
Read more >
Dynamic caching policy with Polly
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or ...
Read more >
Selectively Caching a HttpResponseMessage with Polly
This article will demonstrate how to selectively cache HttpResponseMessages based on the Http StatusCode of the response. The second will show ...
Read more >
Caching in Polly and the HttpClientFactory - no dogma blog
Polly allows you to cache a response for reuse by a subsequent request, it supports both local an distributed caches, full information can...
Read more >
Cache - App-vNext/Polly GitHub Wiki
ResultTtl : specifies a function that will be used to calculate the ttl based on the TResult item to be cached. This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found