Skip to content

Commit 0dbaffc

Browse files
committed
task: fix absolute expiration when set using timespans
Credit to @Meberem and @Sinhk - fixes #84 - fixes #100 - closes #117 - closes #85
1 parent 8e5e506 commit 0dbaffc

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

LazyCache/CachingService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ object CacheFactory(ICacheEntry entry) =>
9393
new Lazy<T>(() =>
9494
{
9595
var result = addItemFactory(entry);
96+
SetAbsoluteExpirationFromRelative(entry);
9697
EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy<T>(entry.PostEvictionCallbacks);
9798
return result;
9899
});
@@ -136,6 +137,15 @@ object CacheFactory(ICacheEntry entry) =>
136137
}
137138
}
138139

140+
private static void SetAbsoluteExpirationFromRelative(ICacheEntry entry)
141+
{
142+
if (!entry.AbsoluteExpirationRelativeToNow.HasValue) return;
143+
144+
var absoluteExpiration = DateTimeOffset.UtcNow + entry.AbsoluteExpirationRelativeToNow.Value;
145+
if (!entry.AbsoluteExpiration.HasValue || absoluteExpiration < entry.AbsoluteExpiration)
146+
entry.AbsoluteExpiration = absoluteExpiration;
147+
}
148+
139149
public virtual void Remove(string key)
140150
{
141151
ValidateKey(key);
@@ -163,6 +173,7 @@ object CacheFactory(ICacheEntry entry) =>
163173
new AsyncLazy<T>(() =>
164174
{
165175
var result = addItemFactory(entry);
176+
SetAbsoluteExpirationFromRelative(entry);
166177
EnsureEvictionCallbackDoesNotReturnTheAsyncOrLazy<T>(entry.PostEvictionCallbacks);
167178
return result;
168179
});

ReleaseNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release notes for LazyCache #
22

3+
## Version 2.0.5
4+
- Fix #85 and #100 Absolute expiration not working with TimeSpan. Thank you to @Meberem and @Sinhk.
5+
- Fix #124 casing for PackageReference - by @jnyrup
6+
37
## Version 2.0.4
48
- Fix #82 Make constructor resolution specific when using aspnet core dependency injection
59

0 commit comments

Comments
 (0)