Closed oasaleh closed 3 months ago
Hi @oasaleh , I'll look into this, but can you give me your DefaultEntryOptions setup please?
@jodydonetti, thank you. Here's my setup in Startup.cs.
services.AddFusionCache(Constants.TokenCacheName).WithDefaultEntryOptions(opt =>
{
opt.Duration = TimeSpan.MaxValue;
});
Also, I'm initiating the concurrency using await Parallel.ForEachAsync
.
I found the problem.
I can't do this inside the method: IFusionCache cache = cacheProvider.GetCache(Constants.TokenCacheName);
. It must be done in the constructor instead. Now it's working!
I'm glad it works now!
One thing though, to avoid surprising for you. In your factory you are doing:
// Calculate the expiration duration based on the token's ExpiresOn property
expirationDuration = result.ExpiresOn - DateTime.UtcNow;
This does nothing, meaning it's just changing a variable declared outside. If you want to do adaptive caching you need to change the context's options, like this:
// Calculate the expiration duration based on the token's ExpiresOn property
ctx.Options.Duration = result.ExpiresOn - DateTime.UtcNow;
Hope this helps.
This does nothing, meaning it's just changing a variable declared outside.
But I am using it in my options.
options => options.SetDuration(expirationDuration)
This is not being taken into account?
Oh yeah absolutely it is, but when calling the GetOrSet
method, not after the execution of the factory.
Let's try to clear up the flow:
GetOrSet
options => options
lambdaDefaultEntryOptions
, duplicate them, and apply the lambda you specifiedSo there's no way to change a variable used beore to affect options already calculated.
The way to change some options in the factory is via Adaptive Caching as I highlighted, by using ctx.Options.Duration = ...
.
Hope this helps!
Describe the bug
I'm calling a method from multiple threads. This method gets the cache and then looks in it for a value (the same value for all the threads.) Even though
tokenCacheKey
andacquireTokenFunc
passed are the same for all the threads, the factory function (the 2nd param forGetOrSetAsync
) is being called for every thread.To Reproduce
Expected behavior
For the same
tokenCacheKey
, the constructor should have only been invoked once (and accordingly,acquireTokenFunc()
should have been invoked once as well.)Versions
I've encountered this issue on: