You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the GetOrCreateAsync method in a concurrent scenario with the DisableLocalCacheWrite and DisableDistributedCacheWrite flags set, the factory method is sometimes not called.
This leads to inconsistent behavior when attempting to check if a specific key exists in the cache or not.
Expected Behavior
The factory method should always be invoked when the key does not exist in the cache.
Steps To Reproduce
var tasks = new List<Task<(bool, bool)>>();
for (var i = 0; i < 100; i++)
{
tasks.Add(Task.Run(async () =>
{
var exist = true;
var result = await _cacheService.GetOrCreateAsync<object, bool>(
"test_key",
null!,
(_, _) =>
{
exist = false;
return new ValueTask<bool>(default(bool)!);
},
new HybridCacheEntryOptions
{
Flags = HybridCacheEntryFlags.DisableLocalCacheWrite | HybridCacheEntryFlags.DisableDistributedCacheWrite
});
return (exist, result!);
}));
}
var resultsArray = await Task.WhenAll(tasks);
var results = resultsArray.ToList();
Exceptions (if any)
No response
.NET Version
dotnet 9
Anything else?
Microsoft.Extensions.Caching.Hybrid with version 9.3.0
Microsoft.Extensions.Caching.StackExchangeRedis with version 9.0.2
The text was updated successfully, but these errors were encountered:
bezaou
changed the title
HybridCache: Inconsistent Behavior of GetOrCreateAsync in Concurrent Calls with readonly Flags Set
HybridCache: Inconsistent behavior of GetOrCreateAsync in concurrent calls with readonly flags set
Mar 26, 2025
Is there an existing issue for this?
Describe the bug
When using the GetOrCreateAsync method in a concurrent scenario with the DisableLocalCacheWrite and DisableDistributedCacheWrite flags set, the factory method is sometimes not called.
This leads to inconsistent behavior when attempting to check if a specific key exists in the cache or not.
Expected Behavior
The factory method should always be invoked when the key does not exist in the cache.
Steps To Reproduce
Exceptions (if any)
No response
.NET Version
dotnet 9
Anything else?
Microsoft.Extensions.Caching.Hybrid with version 9.3.0
Microsoft.Extensions.Caching.StackExchangeRedis with version 9.0.2
The text was updated successfully, but these errors were encountered: