alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

GetOrAdd(...) fails after GetOrAddAsync(...) #12

Closed baddonkey closed 7 years ago

baddonkey commented 7 years ago

Please, consider the following failing sample unit test:

        [Test]
        public async Task GetOrAddFollowinGetOrAddAsyncTryOut()
        {
            Func<Task<ComplexTestObject>> fetchAsync = () => Task.FromResult(testObject);
            await sut.GetOrAddAsync(TestKey, fetchAsync);
            var actualAsync = await sut.GetAsync<ComplexTestObject>(TestKey);
            Assert.IsNotNull(actualAsync);
            Assert.That(actualAsync, Is.EqualTo(testObject));

            Func<ComplexTestObject> fetchSync = () => testObject;
            sut.GetOrAdd(TestKey, fetchSync);
            var actualSync = sut.Get<ComplexTestObject>(TestKey);
            Assert.IsNotNull(actualSync); // actualSync == null
        }

Note:

I assume that the private method UnwrapLazy is not considering the new AsyncLazy enhancements from September. Would be great if you could apply a fix for that, I would like to use this library in a mixed sync/async environment.

Thanks for sharing this library, great work.

Best regards, BadDonkey

alastairtree commented 7 years ago

Fixed in version 0.7.1, see https://www.nuget.org/packages/lazycache

baddonkey commented 7 years ago

Great, thanks!