-
Notifications
You must be signed in to change notification settings - Fork 217
Remove Gen2 GC based trigger for compacting memory cache #221 #265
Conversation
Do we have any gauge as to whether anyone out there has implemented this API and would be broken? |
/// Attempt to remove the given percentage of the total entries in the cache. | ||
/// </summary> | ||
/// <param name="percentage">The percentage of cache entries to remove.</param> | ||
void Compact(double percentage); |
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.
Can you update the doc comment in MemoryCache
itself to be a proper doc comment? Right now there are some odd ///
comments there but not an actual doc comment.
Caching/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs
Lines 275 to 281 in cd57aea
/// Remove at least the given percentage (0.10 for 10%) of the total entries (or estimated memory?), according to the following policy: | |
/// 1. Remove all expired items. | |
/// 2. Bucket by CacheItemPriority. | |
/// 3. Least recently used objects. | |
/// ?. Items with the soonest absolute expiration. | |
/// ?. Items with the soonest sliding expiration. | |
/// ?. Larger objects - estimated by object graph size, inaccurate. |
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.
Also, the implementation in MemoryCache
doesn't do any argument validation, so that needs to be fixed as well.
/// Attempt to remove the given percentage of the total entries in the cache. | ||
/// </summary> | ||
/// <param name="percentage">The percentage of cache entries to remove.</param> | ||
void Compact(double percentage); |
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.
should this return the number of elements removed? or a bool indicating if any elements were removed?
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.
Since the argument is in percentage, it would probably make more sense to return the percentage of elements removed?
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.
That doesn't seem very informative. Also, percentages are problematic if you remove 0/0.
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.
Also, since we are exposing Compact, should we expose Count on the interface as well? It's not a good way to gauge cache size but I suspect users will downcast and rely on it to determine how much to compact like in #252.
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.
The point of using percentages is that they don't need to know the size. Otherwise Compact would take a specific amount.
Or we shift it so Compact takes a specific number of elements to purge, returns the actual number removed, and expose Count.
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.
Then let's keep using percentages for both the amount to remove and amount removed. In the 0/0 case, just return 0. Returning the number of entry removed is rather strange considering a percentage was passed in. Returning a bool is also very uninformative.
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.
@Eilon except we've punted the decide-when-to-compact duty to the developer, and the number of elements in the cache is one inputs you'd use to decide when and how much to purge.
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.
@JunTaoLuo put this on hold until we can sync in person Monday.
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.
Will probably solicit feedback from @davidfowl and @DamianEdwards as well if possible.
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.
I don't think anyone can ever reason about how much to clear based on the number of items in the cache. Remember, the cache is generally a shared resource. Someone (including our own codes) might have stashed a thousand small objects there. Or 1 really ginormous one.
But yes, let's discuss.
We're going with a different design. I will update the issue and open a new PR. |
@JunTaoLuo Could have a look at my change proposal, |
We'll use it as an input but we're basically separating the concept of memory pressure detection out of the cache and adding a first class way to provide compaction logic via an interface. There will also be a way to trigger a compaction via an API exposed from the cache. Similar so what you delegate can do but less specific. |
#221
Will need approval from @Eilon and @DamianEdwards due to the breaking API change.