-
Notifications
You must be signed in to change notification settings - Fork 217
Add size based eviction to MemoryCache #332
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,24 @@ public static ICacheEntry SetValue( | |
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the size of the cache entry value. | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="size"></param> | ||
public static ICacheEntry SetSize( | ||
this ICacheEntry entry, | ||
long size) | ||
{ | ||
if (size < 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some tests for these checks |
||
{ | ||
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be non-negative."); | ||
} | ||
|
||
entry.Size = size; | ||
return entry; | ||
} | ||
|
||
/// <summary> | ||
/// Applies the values of an existing <see cref="MemoryCacheEntryOptions"/> to the entry. | ||
/// </summary> | ||
|
@@ -149,6 +167,7 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt | |
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; | ||
entry.SlidingExpiration = options.SlidingExpiration; | ||
entry.Priority = options.Priority; | ||
entry.Size = options.Size; | ||
|
||
foreach (var expirationToken in options.ExpirationTokens) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,24 @@ public static MemoryCacheEntryOptions SetPriority( | |
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the size of the cache entry value. | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="size"></param> | ||
public static MemoryCacheEntryOptions SetSize( | ||
this MemoryCacheEntryOptions options, | ||
long size) | ||
{ | ||
if (size < 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does size==0 signify anything? Also, is this "Size in bytes"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to impose what size means. When we use this in response caching, it will be bytes but in theory you can have a memory cache where all the sizes are 1 and in that case size means count. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Size of zero means you can add this entry even when the cache is full. |
||
{ | ||
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be non-negative."); | ||
} | ||
|
||
options.Size = size; | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Expire the cache entry if the given <see cref="IChangeToken"/> expires. | ||
/// </summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange, not sure why vs added this.