-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow passing MemoryCacheEntryOptions at cache insertion time a…
…nd add immediate cache removal - Add options for expiration: - ExpirationMode.ImmediateExpiration which uses a timer to remove items from the cache as soon as they expire (more resource intensive) - ExpirationMode.LazyExpiration (existing default) which removes expired cache items when they are next accessed if they have expired. - Fix #96 AddExpirationToken with CancellationChangeToken is not being honored - Allow callers to pass MemoryCacheEntryOptions that is used at cache insertion time. This allows users to wire up callbacks and expiration tokens that fire at the correct time
- Loading branch information
1 parent
0b32080
commit 218bd23
Showing
10 changed files
with
265 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace LazyCache | ||
{ | ||
public enum ExpirationMode | ||
{ | ||
/// <summary> | ||
/// This is the default for Memory cache - expired items are removed from the cache | ||
/// the next time that key is accessed. This is the most performant, and so the default, | ||
/// because no timers are required to removed expired items, but it does mean that | ||
/// PostEvictionCallbacks may fire later than expected, or not at all. | ||
/// </summary> | ||
LazyExpiration, | ||
|
||
/// <summary> | ||
/// Use a timer to force eviction of expired items from the cache as soon as they expire. | ||
/// This will then trigger PostEvictionCallbacks at the expected time. This uses more resources | ||
/// than LazyExpiration. | ||
/// </summary> | ||
ImmediateExpiration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace LazyCache | ||
{ | ||
public class LazyCacheEntryOptions : MemoryCacheEntryOptions | ||
{ | ||
public ExpirationMode ExpirationMode { get; set; } | ||
public TimeSpan ImmediateAbsoluteExpirationRelativeToNow { get; set; } | ||
|
||
public static LazyCacheEntryOptions WithImmediateAbsoluteExpiration(DateTimeOffset absoluteExpiration) | ||
{ | ||
var delay = absoluteExpiration.Subtract(DateTimeOffset.UtcNow); | ||
return new LazyCacheEntryOptions | ||
{ | ||
AbsoluteExpiration = absoluteExpiration, | ||
ExpirationMode = ExpirationMode.ImmediateExpiration, | ||
ImmediateAbsoluteExpirationRelativeToNow = delay | ||
}; | ||
} | ||
|
||
public static LazyCacheEntryOptions WithImmediateAbsoluteExpiration(TimeSpan absoluteExpiration) | ||
{ | ||
return new LazyCacheEntryOptions | ||
{ | ||
AbsoluteExpirationRelativeToNow = absoluteExpiration, | ||
ExpirationMode = ExpirationMode.ImmediateExpiration, | ||
ImmediateAbsoluteExpirationRelativeToNow = absoluteExpiration | ||
}; | ||
} | ||
} | ||
|
||
public static class LazyCacheEntryOptionsExtension { | ||
public static LazyCacheEntryOptions SetAbsoluteExpiration(this LazyCacheEntryOptions option, DateTimeOffset absoluteExpiration, | ||
ExpirationMode mode) | ||
{ | ||
if (option == null) throw new ArgumentNullException(nameof(option)); | ||
|
||
var delay = absoluteExpiration.Subtract(DateTimeOffset.UtcNow); | ||
option.AbsoluteExpiration = absoluteExpiration; | ||
option.ExpirationMode = mode; | ||
option.ImmediateAbsoluteExpirationRelativeToNow = delay; | ||
return option; | ||
} | ||
|
||
public static LazyCacheEntryOptions SetAbsoluteExpiration(this LazyCacheEntryOptions option, TimeSpan absoluteExpiration, | ||
ExpirationMode mode) | ||
{ | ||
if (option == null) throw new ArgumentNullException(nameof(option)); | ||
|
||
option.AbsoluteExpirationRelativeToNow = absoluteExpiration; | ||
option.ExpirationMode = mode; | ||
option.ImmediateAbsoluteExpirationRelativeToNow = absoluteExpiration; | ||
return option; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.