From 0dbaffcd6ca4cbd0ca8109da9071bef6c7db6f7e Mon Sep 17 00:00:00 2001 From: Alastair Crabtree Date: Sat, 19 Sep 2020 15:37:44 +0100 Subject: [PATCH] task: fix absolute expiration when set using timespans Credit to @Meberem and @Sinhk - fixes #84 - fixes #100 - closes #117 - closes #85 --- LazyCache/CachingService.cs | 11 +++++++++++ ReleaseNotes.md | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index 4623c5d..e1f08fb 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -93,6 +93,7 @@ object CacheFactory(ICacheEntry entry) => new Lazy(() => { var result = addItemFactory(entry); + SetAbsoluteExpirationFromRelative(entry); EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy(entry.PostEvictionCallbacks); return result; }); @@ -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); @@ -163,6 +173,7 @@ object CacheFactory(ICacheEntry entry) => new AsyncLazy(() => { var result = addItemFactory(entry); + SetAbsoluteExpirationFromRelative(entry); EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy(entry.PostEvictionCallbacks); return result; }); diff --git a/ReleaseNotes.md b/ReleaseNotes.md index ec31aa0..aefd43b 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -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