-
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
Added Clear method for IMemoryCache to remove all entries. #44522
Conversation
Added corresponding tests scenarios as well. As part of dotnet#44512
public void Clear() | ||
{ | ||
CheckDisposed(); | ||
if (!_entries.IsEmpty) |
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.
There is a race condition between IsEmpty
and Clear
. I'm not sure if it will be dangerous, though.
Is it possible to just call Clear
(i.e. without the check for empty first)?
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.
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 comment
The 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 Clear
is called and the CD is empty, it just nops. Just as thought which is not finished...
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 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.
But am still open for any changes if required!
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.
This has been fixed with my other PR - #44581
@@ -103,6 +103,7 @@ public partial interface IMemoryCache : System.IDisposable | |||
Microsoft.Extensions.Caching.Memory.ICacheEntry CreateEntry(object key); | |||
void Remove(object key); | |||
bool TryGetValue(object key, out object value); | |||
void Clear(); |
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? 🤔
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue meta data
|
For new API we first need to have an approved API request before a PR is offered. I think we should close this PR for now, or convert it to draft. |
Closing, per the above comment. The process outlined in https://aka.ms/api-review is to get an approved API before submitting a PR. |
There is an already API in the review process => #45593. If possible can we make this PR as a draft and not close ?! If the resulting implementation is going to align with this one, then I am ready to make the required changes to this PR. Thank you! |
I don’t understand the benefit of leaving it open as draft. Our process is that we don’t open PRs for new APIs until the API has been approved. |
@eerhardt oh, ok! 😑 |
To give a little more reasoning: dotnet/runtime has 270+ open PRs right now, which is a lot to manage. We are trying to limit "unactionable" PRs from sitting and collecting dust, so people don't have to remember to ignore them. Once we get to an approved API, we can just as easily reopen this PR to move it forward. |
@eerhardt yeah. Ok. OK. No worries! 😇 Thanks for reply! 😊 |
I've just solved this problem for myself (I needed to reset the cache between running each scenario of my unit tests), like this: public sealed class MemoryCacheManager : IMemoryCache
{
readonly IOptions<MemoryCacheOptions> _optionsAccessor;
IMemoryCache _inner;
public MemoryCacheManager(IOptions<MemoryCacheOptions> optionsAccessor)
{
_optionsAccessor = optionsAccessor;
_inner = new MemoryCache(_optionsAccessor);
}
public void Reset()
{
var old = _inner;
_inner = new MemoryCache(_optionsAccessor);
old.Dispose();
}
public void Dispose() => _inner.Dispose();
public ICacheEntry CreateEntry(object key) => _inner.CreateEntry(key);
public void Remove(object key) => _inner.Remove(key);
public bool TryGetValue(object key, out object value) => _inner.TryGetValue(key, out value);
} And then in app startup: services.Remove(services.Single(x => x.ServiceType == typeof(IMemoryCache)));
services.AddSingleton<MemoryCacheManager>();
services.AddSingleton<IMemoryCache>(provider => provider.GetRequiredService<MemoryCacheManager>()); Then I can just resolve an instance of |
As part of #44512