Skip to content

Type-mismatch-based value replacement never expires #199

@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, when this happens, the new value is seemingly unable to expire. If the old value is not added, or allowed to expire before replacing, it behaves as expected, but when the cache performs an in-situ replacement because of the type mis-match, this never happens.

To Reproduce
I use MSTest, but this should translate to NUnit pretty easily.

        public class TestClass { }

        [TestMethod]
        public async Task Temp4()
        {
            // Arrange
            var time = new FakeTimeProvider();
            var underlyingMemoryCache = new MemoryCache(new MemoryCacheOptions
            {
                Clock = new FakeSystemClock(time),
            });
            var cache = new CachingService(new MemoryCacheProvider(underlyingMemoryCache));

            // Act
            var k = "test_key";
            var vstr = await cache.GetOrAddAsync<string>(k, () => Task.FromResult("val"), TimeSpan.FromMinutes(5)); // commenting out this line (or adding a sufficient time.Advance() to expire this value) allows the test to pass
            var vclass = await cache.GetOrAddAsync<TestClass>(k, () => Task.FromResult(new TestClass()), TimeSpan.FromMinutes(5));

            time.Advance(TimeSpan.FromMinutes(10));

            // Assert
            var vExp = await cache.GetAsync<TestClass>(k);
            Assert.IsNull(vExp);
        }

Expected behavior
The new value should have expired after reaching its absolute expiration time.

** 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