Is there a way to overwrite existing cache belongs to a context in Polly cache?
See original GitHub issueSummary: What are you wanting to achieve? I am new to Polly and I am using Polly to save response from API call. I am using .net standard library project. I am able to save cache. But I need to overwrite cache item after any update occurred to that resource.
What code or approach do you have so far?
This is how I configured for polly caching.
services.AddMemoryCache();
services.AddSingleton<IAsyncCacheProvider, MemoryCacheProvider>();
services.AddSingleton<IReadOnlyPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
{
var registry = new PolicyRegistry();
registry.Add("exchangeCachePolicy",
Policy.CacheAsync(
serviceProvider
.GetRequiredService<IAsyncCacheProvider>()
.AsyncFor<List<OutlookCategory>>(),
new SlidingTtl(TimeSpan.FromMinutes(5))));
return registry;
});
I used following code to read from cache
var existingMasterCategories = await _policyRegistry.Get<IAsyncPolicy<List<OutlookCategory>>>("exchangeCachePolicy")
.ExecuteAsync(async context => await GetMasterCategoriesAsync(exchangeUserId), new Context($"GetOutlookMasterCategories-{exchangeUserId}"));
It is always useful to see:
- code of any policy declarations,
- code of the calls to
.Execute/AndCapture/Async(...)
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
[Question] How to invalidate/overwrite a cache value with ...
Hello, I am trying to invalidate a cache value but I can't see a way. My scenario is: I access the cache and...
Read more >Polly cache policy is not adding values to the cache
I think I found the answer in the source code. In the class CacheEngine there is the following code. if (ttl.Timespan > TimeSpan....
Read more >Using Execution Context in Polly
This precludes a dependency injection approach where you define policies centrally on startup, then provide them by DI to the point of use....
Read more >Polly
How to Invalidate a Cache item using Polly [Add Remove and RemoveAsync to cache ... Is there a way to overwrite existing cache...
Read more >Caching in Polly and the HttpClientFactory - no dogma blog
First let's look at how to use the Cache Policy using PolicySelector method on the HttpClientFactory to apply the policy to the request,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@martincostello Above is the part of the method I am using to integrate with polly cache
[martincostello] Edited formatting for readability
Thanks @martincostello , I will try for this.