-
Notifications
You must be signed in to change notification settings - Fork 159
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
Using ExpirationMode.ImmediateEviction ignores sliding expiration value #156
Comments
I suspect the bug is because the immediate expiration stuff was never tested with sliding expiration. Looking at https://github.com/alastairtree/LazyCache/blob/master/LazyCache/Providers/MemoryCacheProvider.cs#L55 you can see it seems to cancel based on absolute expiration so some additional code would be needed detect sliding to make it work.
and note the related code in https://github.com/alastairtree/LazyCache/blob/master/LazyCache/MemoryCacheEntryOptionsExtensions.cs |
Just released a new version, may well fix this. Let us know how you get on? Install-Package LazyCache -Version 2.4.0 |
Unfortunately this doesn't fix it A workaround is to use MemoryCacheEntryOptions instead of LazyCacheEntryOptions |
Hi @ExLuzZziVo, i am also seeing this issue please could you detail the workaround ? |
A PR with a failing unit test would help track this down if you are willing? |
Just replace new LazyCacheEntryOptions {ExpirationMode = ExpirationMode.ImmediateEviction} with new MemoryCacheEntryOptions() in above example and it will work as expected
I will try to make a PR next week |
@alastairtree There is a strange thing with the unit test for this issue. If I run the test method separately, it fails. If I run all the tests, it passes. I have tried to set the cache key manually but the result was the same. [Test]
public void GetFromCacheTwiceAtSameTimeOnlyAddsOnceWithSliding()
{
var times = 0;
var lazyCacheEntryOptions = new LazyCacheEntryOptions { ExpirationMode = ExpirationMode.ImmediateEviction }
.SetSlidingExpiration(TimeSpan.FromSeconds(20));
var t1 = Task.Factory.StartNew(() =>
{
sut.GetOrAdd(TestKey, () =>
{
Interlocked.Increment(ref times);
return new DateTime(2001, 01, 01);
}, lazyCacheEntryOptions);
});
var t2 = Task.Factory.StartNew(() =>
{
sut.GetOrAdd(TestKey, () =>
{
Interlocked.Increment(ref times);
return new DateTime(2001, 01, 01);
}, lazyCacheEntryOptions);
});
Task.WaitAll(t1, t2);
Assert.AreEqual(1, times);
} |
Describe the bug
Hi, I am trying to use ExpirationMode.ImmediateEviction with sliding expiration in my project and this makes the entry to update every time I access it, ignoring the sliding expiration value
To Reproduce
Expected behavior
The sliding expiration time shouldn't be ignored
** Framework and Platform
The text was updated successfully, but these errors were encountered: