Skip to content

Mixing sync and async calls prevents type-mismatches replacing old cache values #200

@CSymes

Description

@CSymes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions