LazyCache
LazyCache copied to clipboard
Mixing sync and async calls prevents type-mismatches replacing old cache values
Describe the bug
If a value with an incompatible type is stored into the cache with GetOrAddAsync, the cache automatically calls the factory and stores the new, correct, value.
However, if you mix the sync and async methods, this does not happen. Using a sync/sync or async/async progression behaves as expected (old value replaced with value produced by the new value), but when mixing the two, the second call returns default.
To Reproduce I use MSTest, but this should translate to NUnit pretty easily.
public class TestClass { }
[TestMethod]
public async Task Temp5()
{
// Arrange
var cache = new CachingService();
// Act
var k = "test_key";
var vstr = cache.GetOrAdd<string>(k, () => "val");
var vclass = await cache.GetOrAddAsync<TestClass>(k, () => Task.FromResult(new TestClass()));
// Assert
var cached = await cache.GetAsync<TestClass>(k);
Assert.IsNotNull(vclass);
Assert.IsNotNull(cached);
}
Expected behavior Should have replaced the previously cached value with the value produced by the new factory.
** Framework and Platform
- OS: Windows 11
- Framework: .NET Core 8
- LazyCache Version: 2.4.0