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

Bugfix : Fixed default cache duration not getting applied to instance methods #190

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
50 changes: 50 additions & 0 deletions LazyCache.UnitTestsCore21/CachingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using NUnit.Framework;
using System.Threading.Tasks;
using System;

namespace LazyCache.UnitTestsCore21
{
Expand Down Expand Up @@ -32,5 +34,53 @@ public void GetOrAddOnCore21ReturnsTheCachedItem()
Assert.IsNotNull(cachedResult);
Assert.AreEqual("SomeValue", cachedResult.SomeProperty);
}

[Test]
public async Task GetOrAddAsyncOnCore21DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}

[Test]
public async Task GetOrAddOnCore21DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAdd("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}
}
}
50 changes: 50 additions & 0 deletions LazyCache.UnitTestsCore22/CachingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using NUnit.Framework;
using System.Threading.Tasks;
using System;

namespace LazyCache.UnitTestsCore22
{
Expand Down Expand Up @@ -32,5 +34,53 @@ public void GetOrAddOnCore22ReturnsTheCachedItem()
Assert.IsNotNull(cachedResult);
Assert.AreEqual("SomeValue", cachedResult.SomeProperty);
}

[Test]
public async Task GetOrAddAsyncOnCore22DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}

[Test]
public async Task GetOrAddOnCore22DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAdd("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}
}
}
50 changes: 50 additions & 0 deletions LazyCache.UnitTestsCore30/CachingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using NUnit.Framework;
using System.Threading.Tasks;
using System;

namespace LazyCache.UnitTestsCore30
{
Expand Down Expand Up @@ -32,5 +34,53 @@ public void GetOrAddOnCore30ReturnsTheCachedItem()
Assert.IsNotNull(cachedResult);
Assert.AreEqual("SomeValue", cachedResult.SomeProperty);
}

[Test]
public async Task GetOrAddAsyncOnCore30DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}

[Test]
public async Task GetOrAddOnCore30DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAdd("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}
}
}
50 changes: 50 additions & 0 deletions LazyCache.UnitTestsCore31/CachingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using NUnit.Framework;
using System.Threading.Tasks;
using System;

namespace LazyCache.UnitTestsCore31
{
Expand Down Expand Up @@ -32,5 +34,53 @@ public void GetOrAddOnCore31ReturnsTheCachedItem()
Assert.IsNotNull(cachedResult);
Assert.AreEqual("SomeValue", cachedResult.SomeProperty);
}

[Test]
public async Task GetOrAddAsyncOnCore31DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}

[Test]
public async Task GetOrAddOnCore31DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAdd("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}
}
}
50 changes: 50 additions & 0 deletions LazyCache.UnitTestsNet50/CachingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LazyCache.Providers;
using Microsoft.Extensions.Caching.Memory;
using NUnit.Framework;
using System.Threading.Tasks;
using System;

namespace LazyCache.UnitTestsNet50
{
Expand Down Expand Up @@ -32,5 +34,53 @@ public void GetOrAddOnNet50ReturnsTheCachedItem()
Assert.IsNotNull(cachedResult);
Assert.AreEqual("SomeValue", cachedResult.SomeProperty);
}

[Test]
public async Task GetOrAddAsyncOnNet50DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}

[Test]
public async Task GetOrAddOnNet50DefaultCacheDurationHonoured()
{
sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1;

int value = DateTime.UtcNow.Second;
int result = await sut.GetOrAdd("foo", x => Task.FromResult(value));

Assert.AreEqual(value, result);

// wait for the item to expire
await Task.Delay(TimeSpan.FromSeconds(2));

// same key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("foo", x => Task.FromResult(value));
Assert.AreEqual(value, result);

// new key
value = DateTime.UtcNow.Second;
result = await sut.GetOrAdd("bar", x => Task.FromResult(value));
Assert.AreEqual(value, result);
}
}
}
4 changes: 2 additions & 2 deletions LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public virtual bool TryGetValue<T>(string key, out T value)

public virtual T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory)
{
return GetOrAdd(key, addItemFactory, null);
return GetOrAdd(key, addItemFactory, DefaultCachePolicy.BuildOptions());
}

public virtual T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory, MemoryCacheEntryOptions policy)
Expand Down Expand Up @@ -180,7 +180,7 @@ public virtual void Remove(string key)

public virtual Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory)
{
return GetOrAddAsync(key, addItemFactory, null);
return GetOrAddAsync(key, addItemFactory, DefaultCachePolicy.BuildOptions());
}

public virtual async Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory,
Expand Down