Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy should be supplied when refreshing the cache due to type mismatch #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ public void GetOrAddAndThenGetOrAddDifferentTypeDoesLastInWins()
Assert.IsInstanceOf<ComplexTestObject>(second);
}

[Test]
public void GetOrAddAndThenGetOrAddDifferentTypeSuppliesPolicy()
{
sut.GetOrAdd(TestKey, cacheEntry => 123);
sut.GetOrAdd(TestKey, cacheEntry => "itemWithDifferentType", new MemoryCacheEntryOptions { SlidingExpiration = new TimeSpan(750) });
Thread.Sleep(1500);
Assert.IsNull(sut.Get<string>(TestKey));
}

[Test]
public void GetOrAddAndThenGetValueObjectReturnsCorrectType()
{
Expand Down Expand Up @@ -337,6 +346,15 @@ public async Task GetOrAddAsyncAndThenGetOrAddAsyncDifferentTypeDoesLastInWins()
Assert.IsInstanceOf<ComplexTestObject>(second);
}

[Test]
public async Task GetOrAddAsyncAndThenGetOrAddAsyncDifferentTypeSuppliesPolicy()
{
await sut.GetOrAddAsync(TestKey, cacheEntry => Task.FromResult(new object()));
await sut.GetOrAddAsync(TestKey, cacheEntry => Task.FromResult("itemWithDifferentType"), new MemoryCacheEntryOptions { SlidingExpiration = new TimeSpan(750) });
await Task.Delay(1500);
Assert.IsNull(sut.Get<string>(TestKey));
}

[Test]
public async Task GetOrAddAsyncAndThenGetAsyncWrongObjectReturnsNull()
{
Expand Down
4 changes: 2 additions & 2 deletions LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ object CacheFactory(ICacheEntry entry) =>

try
{
cacheItem = CacheProvider.GetOrCreate<object>(key, CacheFactory);
cacheItem = CacheProvider.GetOrCreate<object>(key, policy, CacheFactory);
}
finally
{
Expand Down Expand Up @@ -226,7 +226,7 @@ object CacheFactory(ICacheEntry entry) =>

try
{
cacheItem = CacheProvider.GetOrCreate<object>(key, CacheFactory);
cacheItem = CacheProvider.GetOrCreate<object>(key, policy, CacheFactory);
}
finally
{
Expand Down