Skip to content

Commit

Permalink
task: fix absolute expiration when set using timespans
Browse files Browse the repository at this point in the history
Credit to @Meberem and @Sinhk
- fixes #84
- fixes #100
- closes #117
- closes #85
  • Loading branch information
alastairtree committed Sep 19, 2020
1 parent 8e5e506 commit 0dbaffc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ object CacheFactory(ICacheEntry entry) =>
new Lazy<T>(() =>
{
var result = addItemFactory(entry);
SetAbsoluteExpirationFromRelative(entry);
EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy<T>(entry.PostEvictionCallbacks);
return result;
});
Expand Down Expand Up @@ -136,6 +137,15 @@ object CacheFactory(ICacheEntry entry) =>
}
}

private static void SetAbsoluteExpirationFromRelative(ICacheEntry entry)
{
if (!entry.AbsoluteExpirationRelativeToNow.HasValue) return;

var absoluteExpiration = DateTimeOffset.UtcNow + entry.AbsoluteExpirationRelativeToNow.Value;
if (!entry.AbsoluteExpiration.HasValue || absoluteExpiration < entry.AbsoluteExpiration)
entry.AbsoluteExpiration = absoluteExpiration;
}

public virtual void Remove(string key)
{
ValidateKey(key);
Expand Down Expand Up @@ -163,6 +173,7 @@ object CacheFactory(ICacheEntry entry) =>
new AsyncLazy<T>(() =>
{
var result = addItemFactory(entry);
SetAbsoluteExpirationFromRelative(entry);
EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy<T>(entry.PostEvictionCallbacks);
return result;
});
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release notes for LazyCache #

## Version 2.0.5
- Fix #85 and #100 Absolute expiration not working with TimeSpan. Thank you to @Meberem and @Sinhk.
- Fix #124 casing for PackageReference - by @jnyrup

## Version 2.0.4
- Fix #82 Make constructor resolution specific when using aspnet core dependency injection

Expand Down

0 comments on commit 0dbaffc

Please sign in to comment.