-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Clear method for IMemoryCache to remove all entries. #44522
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 |
---|---|---|
|
@@ -290,6 +290,16 @@ public void Remove(object key) | |
StartScanForExpiredItems(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Clear() | ||
{ | ||
CheckDisposed(); | ||
if (!_entries.IsEmpty) | ||
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. There is a race condition between 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. Ohh, yes we can call directly Clear(), but I added that check just to avoid unnecessary calls to Clear(). 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. Maybe it would be better if the concurrent dicitonary (CD) handles this case. I mean if 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. I checked the implementation of Clear() method, I did not see any check there, everytime it's initializing new table under the hood with given size! So I think it's better to have this check here. 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. This has been fixed with my other PR - #44581 |
||
{ | ||
_entries.Clear(); | ||
} | ||
} | ||
|
||
private void RemoveEntry(CacheEntry entry) | ||
{ | ||
if (EntriesCollection.Remove(new KeyValuePair<object, CacheEntry>(entry.Key, entry))) | ||
|
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.
@MCCshreyas - thanks for the contribution!
However, since this is public API we need to follow the API review process before accepting changes in this area. See the link at the top of this file: https://aka.ms/api-review process. We won't be able to merge this PR without first getting an API approval.
An issue with the currently proposed changes is that IMemoryCache is an interface, and adding more members to an interface is a binary breaking change. This type of change isn't acceptable in these libraries.
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.
So need to come up with new interface for a single method? 🤔